I.T. Discussion Community!
-Collapse +Expand
VB Classic
Search VB Classic Group:

Advanced
-Collapse +Expand VB Classic Store

Prestwood eMagazine

December 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 8 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 51 months ago

Comment 2 of 3

Just few people are comments in KB articles because mostly people are don’t understand the KB articles. Mostly students are read the reviews on paper writing services because its easy to understand for everyone.

Posted 11 days ago

Comment 3 of 3

Giving the views and opinions on the other things are special so that you can give your interest in that field. All over the world Pure Forskolin Extract Reviews are written by the best professionals.

Posted 9 days 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 = P1156A1
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
    11177
    Since 2/11/2009
    -
       Contact Us!
     
    PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

    1,632 People Online Now!!  
    Sign In to see who's online now!  Not a member? Join now. It's free!
    Show more stats...


    ©1995-2017 PrestwoodBoards  [Security & Privacy]
    Professional IT Services: Coding | Websites | Computer Tech