Here is another one.
Over the weekend, I got a crash in "Nextelement" subroutine in the "mutations" module
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 And beginning < uboundarray 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
for some reason 'k' was getting bigger than the maximum array size (defined in 'uboundarray')
Below is the fix for it.
By adding a conditional to exit the while loop if 'k' exceeds the allowed value, we can prevent the crash. Not sure if this will cause other problems further down the line by the function returning the wrong value. Shouldn't though because the conditional outside the while loop should set 'nextelement' to -1 anyway.
on jumping out of the while loop, 'k' has to first be set to a usable value so I set it to the maximum possible value of 'uboundarray'.
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 And beginning < uboundarray 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
If k > uboundarray Then k=uboundarray GoTo jumpout
Wend
jumpout:
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