Reputation: 65
I have run into an if statement that acts as if it is true even if it evaluates to false, and I cannot figure out what is going on. The code is as follows:
If (k = j) Then
Debug.Print "Code being stupid"
ReDim pinInfo(UBound(textConn))
For i = 0 To UBound(textConn)
For j = 0 To UBound(colConn)
If (StrComp(textConn(i), colConn(j), vbBinaryCompare) = 0 _
And StrComp(textPos(i), colPos(j), vbBinaryCompare) = 0) Then
pinInfo(i) = textConn(i) & "," & textPos(i) & "," & label(i) & "," & colScl(j)
'Debug.Print pinInfo(i)
Exit For
End If
Next j
Next i
Else
Debug.Print "k <> j"
MsgBox "The number of shapes representing the wire ID does not equal the number of shpaes representing the wire color."
Exit Sub
End If
j and k are both integers. At this point in the program they should always be equal. I intentionally goobered up the input so that they are not, but even though the statement evaluates to false it still acts as if it returned true. The picture below is a screenshot of what I see after execution. As you can see, it shows that the condition is false, yet executes the first print statement and no message box appears.
Upvotes: 1
Views: 2079
Reputation: 350252
When j = k
the if
block is executed. The debug output is proof of that. At your break point j
is no longer equal to k
.
This is because the already executed if
block has modified j
here:
For j = 0 To UBound(colConn)
After the if
block the code jumps to the End If
where you have the break point set. Only then you look at the expression j = k
, but be aware that since j
changed in the mean time, this was not the outcome when the if
statement was executed.
Upvotes: 2