IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
VB Classic
Search VB Classic Group:

Advanced
-Collapse +Expand VB Classic Store

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingVB ClassicLanguage Basics   Print This     
  From the January 2016 Issue of Prestwood eMag
 
VB Classic Language Basics:
VB Classic Logical Operators (and, or, not)
 
Posted 15 years ago on 2/11/2009 and updated 9/15/2009
VB Classic Code Snippet:
 A flashcard from our VB Classic Flashcards Library
 A code snippet from our VB Classic Code Snippets Page

KB101892

Languages Focus: Logical Operators

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 and, as in this and that
or or, as in this or that
Not 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.

Syntax Example:
'Given expressions a, b, c, and d:
If Not (a and b) and (c or d) Then
  'Do something.
End If

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.

  1. Create a new form and place a button and a PictureBox on it. Add a picture to the PictureBox with the Picture property.
     
    Demo of using not operator.
     
  2. 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

     
  3. 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

More Info


Comments

1 Comments.
Share a thought or comment...
Comment 1 of 3

Great example of using NOT.

---
Jonathan
Posted 11 years ago

Comment 2 of 3

Are you looking for a healthy diet plan fibromyalgia diet

Posted 48 months ago

Comment 3 of 3
https://url4.org/jtvti
https://url4.org/dhahn
https://url4.org/ybudp
https://url4.org/pmzwx
https://url4.org/pnegi
Posted 47 months ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P146A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile


Linked Certification Question(s)

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.

Beginner

1 Beginner Level Question

Question #1: True or False?

Given the following code, which MsgBox will display? The True or the False one?

Dim a, b, c, d As Boolean
  
a = True
b = True
c = True
d = False
 
If Not (a And b) And (c Or d) Then
  MsgBox ("True")
Else
  MsgBox ("False")
End If
Answer:
  • True
  • False
  • Intermediate

    1 Intermediate Level Question

    Question #2: Yes or No?

    Given the following two functions:

    Function This()
        This = False
    End Function
     
    Function That()
        That = True
    End Function

     

    Will the That function in the following code execute?

    Private Sub Command0_Click()
        If This And That Then
            MsgBox ("hi")
        End If
    End Sub
    Answer:
  • Yes
  • No

  •  KB Article #101892 Counter
    19434
    Since 2/11/2009
    Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
    www.prestwood.com


    ©1995-2024 Prestwood IT Solutions.   [Security & Privacy]