VB .NET features two new logical operators that help make your programming ... well ... more logical. The new operators are AndAlso and OrElse and they generally replace the VB 6 operators And and Or (but they do much more!).
The old VB 6 And and Or are still present in VB .NET. If you decide that they're what you need your program, go ahead and use them. They work the same way they used to work.
But AndAlso and OrElse have some properties that enhance your code in ways that VB 6 can't match. They offer advantages in two general categories:
- You can avoid executing part of a logical expression to avoid problems.
- You can optimize code by not executing any more of a compound expression than required.
AndAlso and OrElse are pretty much like And and Or except that they will "short circuit" an expression once the outcome is guaranteed. Suppose you're coding a test of a calculation result like this:
If a <>
If the variables in the block have these values:
Value1 = 1
Value2 = 1
Value3 = 0
a = 1
b = 1
The If expression generates a "divide by zero" error in VB 6 because Value3 is zero. It could be that the cases that result in Value3 being zero are very rare and only occur when you're enjoying a vacation a thousand miles away so you can be called back to fix the program in an emergency mode. (Hey! It happens!)
Let's recode the program as a .NET program using AndAlso and see what happens.
Dim Value1 As Integer
Dim Value2 As Integer
Dim Value3 As Integer
Dim a As Integer
Dim b As Integer
Value1 = 1
Value2 = 1
Value3 = 0
a = 1
b = 1
If a > Value1 AndAlso b > (Value2 / Value3) Then
' {body of If block}
End If
MsgBox("Hey! It works!")
First, of course, we have to declare our variables with VB .NET. Then after changing And to AndAlso, the program works! One reason is that the last part of the compound If condition - (value 2 / value3) - is never actually executed. When you use AndAlso, VB .NET knows that the expression can't succeed once it is determined that the first part of the condition - a is not greater than Value1 - is false. So VB .NET stops evaluating the expression right there.
Knowledgeable .NET'ers will point out that VB .NET supports a value for infinity - which is what the result of division by zero actually is in mathematical terms - so the expression would work anyway. Very true! But consider this example where our program tests a value obtained directly from the user:
Dim Value1 As Integer
Dim a As Integer
Dim b As String
Value1 = 1
a = 1
b = 1
If a > Value1 AndAlso b = Console.ReadLine() Then
' {body of If block}
End If
MsgBox("Hey! It works!")
In VB 6 (or if you use And in VB .NET), the program will ask for input every time. If you want the second test to be executed every time, use And. If that's not what you want, use AndAlso.
This analysis suggests how you can add some efficiency to your code by arranging a compound logical expression correctly. If you place the expression that is most likely to be false in the leftmost position when using AndAlso, you can prevent execution cycles from being used to evaluate the rightmost expression. In a single test, it wouldn't make enough difference to be worth even thinking about. But if your test is inside a loop of some kind and is executed zillions of times, it could make a big difference.
The OrElse operator has similar differences compared with the VB 6 Or operator. Knowing about these two new VB .NET logical operators can help you avoid very subtle errors or achieve subtle efficiencies.