I finally managed to crash the program. Had to leave it running all night in debug mode to get it to break.
It is the same line of code that was reported earlier.
While Not (DNA(k).tipo = 4 And DNA(k).value = 4) And Not (DNA(k).tipo = tipo And DNA(k).value = value)
From reading the thread above I see that a lot of you may need a little direction in bug fixing/detection so I will go through this one step by step.
Now just saying that this line is a problem is not enough to actually debug this. You need to understand the reason why it goes wrong.
Here is the whole routine.
Public Function NextElement(ByRef DNA() As block, beginning As Integer, tipo As Integer, value As Integer) As Integer
'takes the input for the first value in a gene and returns the position of the next statement
'as defined by tipo and value
Dim k As Integer
Dim uboundarray As Long
uboundarray = UBound(DNA())
If DNA(uboundarray).tipo <> 4 And DNA(uboundarray).value <> 4 Then
ReDim Preserve DNA(uboundarray + 1)
DNA(uboundarray + 1).tipo = 4
DNA(uboundarray + 1).value = 4
End If
k = beginning
If beginning > 0 Then
While Not (DNA(k).tipo = 4 And DNA(k).value = 4) And Not (DNA(k).tipo = tipo And DNA(k).value = value)
k = k + 1
Wend
If Not (DNA(k).tipo = tipo And DNA(k).value = value) Then k = -1
Else 'beginning wasn't valid
k = -1
End If
NextElement = k
End Function
First we have to figure out what is wrong. By hovering the mouse we can see what value is causing the trouble.
You very quickly find that hovering over the first part of the offending code line, it shows "DNA(k).tipo =<Subscript out of range>"
This means that the program is attempting to reference an array element that is outside of the allowed range.
Hold the mouse over k and you will see a value of 3
So how big is the DNA() array anyway? It should be bigger than 3 surely :blink: After all it is supposed to hold the entire DNA of the bot.
Go back up the code until you find a line that reads
uboundarray = UBound(DNA())
Ubound is a function that returns the number of allowable elements in an array so the variable "uboundarray" should hold that value.
Hover the mouse over it and you will see 2 :blink:
Somehow the entire DNA of this robot is only 2 elements long.
We can fix this routine by adding a conditional that kicks it back out of the routine if the value of uboundarray is less than 3 but that only fixes a symptom.
What the heck is the actual problem?
We need to trace it back further than this. What other routine is calling this one?
To find out, click the binoculars on the VB toolbar and type in the name of this routine into the search box. Select "current project" and click "find next". If you keep pressing it, you will switch through every instance of this name in the entire code. You will quickly notive that "NextElement" is called from loads of different places. How to figure out which one called it?
It's actually quite easy. The call contains 4 elements, DNA, inizio and 2 digits. Simply hold the mouse over inizio in each of the calls until you see a value pop up. Only the routine that directly called it will do this so using this technique you can backtrack all the way through the code to find where the values initially came from.
More in a bit. I am still tracing this one myself right now.