Description of "short-circuit" evaluation in Visual Basic

Article translations Article translations
Article ID: 817250 - View products that this article applies to.
Expand all | Collapse all

On This Page

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.

MORE INFORMATION

Sample code in Visual Basic 6.0

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

    By default, a form that is named Form1 is created.
  2. Add two Command buttons to the form.
  3. Right-click Form1, and then click View Code.
  4. Add the following code to the Form1 form.
    Option Explicit
    
    Public Function FalseFunc() As Boolean
       MsgBox ("Function Returning False")
       FalseFunc = False
    End Function
    
    Public Function TrueFunc() As Boolean
       MsgBox ("Function Returning True")
       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 "Both the Functions are called."
       End If
    End Sub
    
  5. On the Run menu, click Start to run the application.
  6. 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.
  7. 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.
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.
  1. Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
  2. Create a new Windows application by using Visual Basic 2005 or Visual Basic .NET.

    By default, a form that is named Form1 is created.
  3. Add two buttons to the Form1 form.
  4. Right-click Form1.vb, and then click View Code.
  5. Append the following code in the Windows Form Designer generated code region.
        Public Function FalseFunc() As Boolean
            MsgBox("Function Returning False")
            FalseFunc = False
        End Function
    
        Public Function TrueFunc() As Boolean
            MsgBox("Function Returning True")
            TrueFunc = True
        End Function
    
  6. 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("Both function are called.")
            End If
        End Sub
  7. Save and then run the project.
  8. 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.
  9. 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.
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:
  1. 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.
  2. Add two buttons to Form1.
  3. Right-click Form1.vb, and then click View Code.
  4. Append the following code in the Windows Form Designer generated code region.
       Private Function TrueFunc() As Boolean
          MessageBox.Show("Function Returning True")
          Return True
       End Function
    
       Private Function FalseFunc() As Boolean
          MessageBox.Show("Function Returning False")
          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("Only TrueFunc is called.")
          End If
       End Sub
    
  5. On the Debug menu, click Start to run the application.
  6. Click Button1.

    The Function returning False message box is displayed.
  7. Click Button2.

    The Function returning True message box is displayed.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/8067cy78(vs.71).aspx

Properties

Article ID: 817250 - Last Review: December 3, 2007 - Revision: 3.6
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
Keywords: 
kbinfo kbprogramming kbforms kbcompiler kbvs2005applies kbvs2005swept KB817250

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com