Logical operators perform conditional and, or, and not operations. Some languages support both binary logical operators that link two and unary logical operators negate (make opposite) the truth value of its argument. Finally, some languages short circuit logic. For example, with this or that, if this is an expression returning true, then that is never executed.
VB Classic Logical Operators
VB Classic logical operators:
and, as in this and that
or, as in this or that
Not, as in Not This
VB Classic never short circuits. Given the expression this or that as well as this and that, if this evaluates to false, then that is still executed.
'Given expressions a, b, c, and d:
If Not (a and b) and (c or d) Then
Using the Logical Not Operator
You can use the not operator in many contexts. One of my favorite uses for it is to toggle boolean properities with a single line of code:
BooleanProperty = Not BooleanProperty
VB6 Working Demo
The following demo uses the not operator to toggle the visible property of a PictureBox from a button.
Create a new form and place a button and a PictureBox on it. Add a picture to the PictureBox with the Picture property.
Edit the Click event and alter it as follows below (double click the button). Your PictureBox should be named Picture1, but if it's not, use the correct object name.
Private Sub Command1_Click() Picture1.Visible = Not Picture1.Visible End Sub
Run the application and test. Select Run| Start and click the button. You'll notice the picture toggles between visible and not visible with a single line of code.
VB Classic Short Circuting Example:
In the following example, if VB Classic supported short circuting, the That function would never execute. It's interesting to note that VB.Net has introduced two new operators to support short cicuiting: AndAlso and OrElse but you'll have to move to VB.Net to take advantage of them.
Function This() MsgBox ("The This function executed.") This = False End Function
Function That() MsgBox ("The That function executed!") That = True End Function
Private Sub Command0_Click() 'Notice both the This and That functions execute 'even though the This Function returned False. If This And That Then MsgBox ("hi") End If End Sub
The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.