Visual Basic.NET 2008 > Programming Fundamentals Short-Circuiting Expression Evaluation (If...Then) - Visual Basic 2008A common pitfall of evaluating expressions with VB is to attempt to compare a Nothing value to something. An object variable that hasn't been set to a value can't be used in calculations or comparisons. Consider the following statements:
These statements create a SolidBrush object variable, the B variable, and then examine the brush color and prohibit the user from drawing with a white brush. The second statement initializes the brush to the cyan color. (Every shape drawn with this brush will appear in cyan.) If you attempt to use the B variable without initializing it, a runtime exception will be thrown: the infamous NullReferenceException. In our example, the exception will be thrown when the program gets to the If statement, because the B variable has no value (it's Nothing), and the code attempts to compare it to something. Nothing values can't be compared to anything. Comment out the secondstatement by inserting a single quote in front of it and then execute the code to see what will happen. Then restore the statement by removing the comment mark. Let's fix it by making sure that B is not Nothing:
The If statement should compare the Color property of the B object, only if the B object is not Nothing. But this isn't the case. The AND operator evaluates all terms in the expression and then combines their results (True or False values) to determine the value of the expression. If they're all True, the result is also True. However, it won't skip the evaluation of some terms as soon as it hits a False value. To avoid unnecessary comparisons, use the AndAlso operator. The AndAlso operator does what the And operator should have done in the first place: It stops evaluating the remaining terms or the expression because they won't affect the result. If one of its operands is False, the entire expression will evaluate to False. In other words, if B is Nothing, there's no reason to compare its color; the entire expression will evaluate to False, regardless of the brush's color. Here's how we use the AndAlso operator:
The AndAlso operator is said to short-circuit the evaluation of the entire expression as soon as it runs into a False value. As soon as one of the parts in an AndAlso operation turns out to be False, the entire expression is False and there's no need to evaluate the remaining terms. There's an equivalent operator for short-circuiting OR expressions: the OrElse operator. The OrElse operator can speed the evaluation of logical expressions a little, but it's not as important as the AndAlso operator. Another good reason for short-circuiting expression evaluation is tohelp performance. If the second term of an And expression takes longer to execute (it has to access a remote database, for example), you can use the AndAlso operator to make sure that it's not executed when not needed. Table of Contents
|
|||||
W3computing.com Copyright 2011 © All Rights Reserved
|
|||||
| Home | Useful links | Contact us | |||||