CodeSeeker

Just another .NET developer trying to do the right thing
posts - 13, comments - 14, trackbacks - 0

My Links

News

Archives

Blogs

Short-circuit IF statements in VB.NET with AndAlso


In-line checking of values during the evaluation of a compound IF statement in VB.NET can be done as a way of "short-circuiting" the IF statement in case further evaluation might cause errors. For example,
Dim blah As Nullable(Of Integer) = Nothing
If blah.HasValue And blah.Value < 5 Then
    Console.WriteLine("Something really important")
End If
In this case, the IF statement will cause an error. The AND operator in VB causes all clauses prior to the THEN statement to be evaluated. So both the "blah.HasValue" and "blah.Value < 5" clauses will be evaluated. "blah.HasValue" is false but evaluation continues to the next clause. Then "blah.Value < 5" causes a "System.InvalidOperationException: Nullable object must have a value."

There are two ways to deal with this situation. One is with a nested IF statement, like this:
Dim blah As Nullable(Of Integer) = Nothing
If blah.HasValue Then
    If blah.Value < 5 Then
         Console.WriteLine( _
             "Something really important")
     End If
End If
In this case, the second IF statement will not be evaluated if the first IF statement does not pass.

Another, more elegant way to deal with this situation is to use the VB.NET AndAlso operator. With this operator, the IF statement does not go on to the next clause if the first clause is not true; in effect "short-circuiting" the IF statement.
Dim blah As Nullable(Of Integer) = Nothing
If blah.HasValue AndAlso blah.Value < 5 Then
    Console.WriteLine("Something really important")
End If
Now, the second clause of the IF statement will not be evaluated because the first clause returned False, and an exception will not occur.

Print | posted on Monday, July 23, 2007 9:03 AM |

Feedback

Gravatar

# re: Short-circuit IF statements in VB.NET with AndAlso

This is the default behavior of C#. Would you recommend that "AndAlso" be used in almost all cases? It seems that one would not want to evaluate the second second clause if the first is false, because the the condition of the 2nd clause would be irrelevant at the point.
7/23/2007 2:51 PM | Dave C.
Gravatar

# re: Short-circuit IF statements in VB.NET with AndAlso

You are exactly right. The condition of the 2nd clause would be irrelevant. Coming from C#, it confused me why VB behaved this way and I solved this problem with nested IF's until I learned about AndAlso.

Using "And" instead of "AndAlso" saves 4 characters of typing in situations where you don't need to short-circuit the IF statement. And even then, it will still work, and work quicker because it may not need to evaluate all of the clauses.
7/23/2007 3:01 PM | Mike Ellis
Gravatar

# re: Short-circuit IF statements in VB.NET with AndAlso

They should've done this backwards. Have the AND short-circuit and have the AndAlso evaluate both.

It's rare to ever want both evaluated so the default should be to short-circuit.

3/25/2008 1:21 PM | Todd
Gravatar

# re: Short-circuit IF statements in VB.NET with AndAlso

Todd,

I agree with you, short-circuit should be the defacto as c# does it.

6/1/2009 9:34 AM | Keith S

Post Comment

Title  
Name  
Email
Url
Comment   

Powered by: