Microsoft KB Archive/817250

= Description of &quot;short-circuit&quot; evaluation in Visual Basic =

Article ID: 817250

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft .NET Framework 2.0
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-





SUMMARY
The operands of logical-AND expressions and logical-OR expressions are evaluated from left to right. If the value of the first operand is sufficient to determine the result of the operation, the second operand is not evaluated. This is refered to as short-circuit evaluation. This article discusses short-circuit evaluation of the logical-AND operator and of the logical-OR operator in Microsoft Visual Basic 2005 or in Microsoft Visual Basic .NET.

In Visual Basic 2005 or in Visual Basic .NET, both the operands of logical-AND expressions and logical-OR expressions are evaluated, irrespective of the result of the first operand. These operators do not behave as they do in Microsoft Visual C# .NET. Instead these operators behave similar to the way they behave in Microsoft Visual Basic 6.0. Therefore, the logical-AND operator and the logical-OR operator do not follow short-circuit evaluation. However, there are two new operators in Visual Basic 2005 or in Visual Basic .NET. These are the AndAlso operator and the OrElse operator. You can use these operators instead of the logical-AND operator and the logical-OR operator, respectively. You can have short-circuit evaluation functionality in Visual Basic .NET by using these operators.



Sample code in Visual Basic 6.0
The following proceedure demonstrates short-circuit evaluation in Visual Basic 6.0.  Start Visual Basic 6.0, and then create a new Standard EXE project.

By default, a form that is named Form1 is created. Add two Command buttons to the form. Right-click Form1, and then click View Code.  Add the following code to the Form1 form. Option Explicit

Public Function FalseFunc As Boolean MsgBox (&quot;Function Returning False&quot;) FalseFunc = False End Function

Public Function TrueFunc As Boolean MsgBox (&quot;Function Returning True&quot;) TrueFunc = True End Function

Private Sub Command1_Click If FalseFunc And TrueFunc Then ' Do Nothing End If End Sub

Private Sub Command2_Click If TrueFunc Or FalseFunc Then MsgBox &quot;Both the Functions are called.&quot; End If End Sub  On the Run menu, click Start to run the application. Click Command1.

Messages boxes display Function Returning False and Function Returning True. These messages indicate that both functions are running, but the first operand returns False. Click Command2.

Message boxes display Function Returning True and Function Returning False. These messages indicate that both functions are running, but the first operand returns True.</li></ol>

In Visual Basic 6.0, the output indicates that the logical-AND operator and the logical-OR operator always evaluate all the expressions in their operands. When the first operand of the logical-AND operator evaluates to False, the result always evaluates to False. This occurs irrespective of the value of the second operand. In such cases, it may seem more efficient to skip processing the expressions of the second operand. Similarly, if the first operand evaluates to True for the logical-OR operator, the remaining expression always evaluates to True. Therefore, the compiler can skip evaluating the remaining operands to create an optimized code. However, you can use this Visual Basic 6.0 feature to perform some processing that must always be completed, irrespective of the value of the first operand.

Sample code in Visual Basic 2005 or in Visual Basic .NET
The following section demonstrates short-circuit evaluation in Visual Basic 2005 or in Visual Basic .NET. <ol> Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.</li> Create a new Windows application by using Visual Basic 2005 or Visual Basic .NET.

By default, a form that is named Form1 is created.</li> Add two buttons to the Form1 form.</li> Right-click Form1.vb, and then click View Code.</li>  Append the following code in the Windows Form Designer generated code region. Public Function FalseFunc As Boolean MsgBox(&quot;Function Returning False&quot;) FalseFunc = False End Function

Public Function TrueFunc As Boolean MsgBox(&quot;Function Returning True&quot;) TrueFunc = True End Function </li>  Add the following event handlers for the Click events of each button. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If FalseFunc And TrueFunc Then 'Do Nothing End If   End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If TrueFunc Or FalseFunc Then MsgBox(&quot;Both function are called.&quot;) End If   End Sub </li> Save and then run the project.</li> Click Button1.

Message boxes display Function Returning False and Function Returning True. These messages indicate that both functions are running, but the first operand returns False.</li> Click Button2.

Message boxes display Function Returning True and Function Returning False. These messages indicate that both functions are running, but the first operand returns True.</li></ol>

The output indicates that the logical-AND operator and the logical-OR operator are treated the same way in Visual Basic 2005 or in Visual Basic .NET as they are treated in Visual Basic 6.0. By default, short-circuit evaluation does not occur with Visual Basic 2005 or with Visual Basic .NET with the logical-AND operator and the logical-OR operator.

Short-circuit evaluation in Visual Basic 2005 or in Visual Basic .NET
In Visual Basic 2005 or in Visual Basic .NET, you can use the AndAlso operator and the OrElse operator instead of the logical-AND operator and the logical-OR operator, respectively. You can have short-circuit evaluation functionality in Visual Basic 2005 or in Visual Basic .NET by using these new operators. The following steps demonstrate short-circuit evaluation in Visual Basic 2005 or in Visual Basic .NET: <ol> Start Visual Studio 2005 or Visual Studio .NET, and then create a new Windows application by using Visual Basic 2005 or Visual Basic .NET.

By default, Form1 is created.</li> Add two buttons to Form1.</li> Right-click Form1.vb, and then click View Code.</li>  Append the following code in the Windows Form Designer generated code region. Private Function TrueFunc As Boolean MessageBox.Show(&quot;Function Returning True&quot;) Return True End Function

Private Function FalseFunc As Boolean MessageBox.Show(&quot;Function Returning False&quot;) Return False End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If FalseFunc AndAlso TrueFunc Then ' Do Nothing End If  End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If TrueFunc OrElse FalseFunc Then MsgBox(&quot;Only TrueFunc is called.&quot;) End If  End Sub </li> On the Debug menu, click Start to run the application.</li> Click Button1.

The Function returning False message box is displayed.</li> <li>Click Button2.

The Function returning True message box is displayed.</li></ol>

<div class="references_section">