Entry
Does Visual Basic have "short-circuit" boolean evaluation like in Java and C++?
Mar 21st, 2001 14:46
Mike Ogilvie, Joe T,
Short Answer: No
Long Answer:
Let's make sure we're defining "short-circuit" the same way. Take this
section of code...
If (Not IsEmpty(vObject)) And (vObject.Name = "DataList") Then
vObject.DoSomething
End If
The idea here would be to first make sure that there's actually
something in vObject. If there is, then to make sure it's the correct
name. In the above If statement, it would check the first condition,
find out that it fails, and then "short-circuit" the process by not
bothering to check any remaining conditions.
Visual Basic does not do this. It evaluates everything in the line of
code, regardless if it logically needs to or not. So in VB, the above
code should be written...
If Not IsEmpty(vObject) Then
If vObject.Name = "DataList" Then
vObject.DoSomething
End If
End If
More of a hassle, I know. But these situations don't come around too
often. If they do, you can probably architect the application to handle
it more efficiently.