Results 1 to 6 of 6

Thread: vb.net compound if statement. how?

  1. #1

    Thread Starter
    Addicted Member darth vador's Avatar
    Join Date
    Jul 2005
    Posts
    148

    vb.net compound if statement. how?

    Grrrrrrrrrrr... I keep getting errors.

    I can't figure out how to use the & / or operators in an if statement.

    Code:
    if (x < Y) & (y > Z) then
    
    ... yadda
    
    end if
    ???

  2. #2
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: vb.net compound if statement. how?

    try AND instead of &

  3. #3
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: vb.net compound if statement. how?

    VB Code:
    1. If X < Y And Y > Z Then
    2.   'do code if both is true
    3. End If
    4.  
    5. If X < Y Or Y > Z Then
    6.   'do code if one is true
    7. End If

  4. #4

    Thread Starter
    Addicted Member darth vador's Avatar
    Join Date
    Jul 2005
    Posts
    148

    Re: vb.net compound if statement. how?

    Thanks guys.

  5. #5
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: vb.net compound if statement. how?

    You can also use the commonly forgotten( AndAlso OrElse) operators.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: vb.net compound if statement. how?

    You absolutely SHOULD use AndAlso and OrElse. Those from a VB6 background are used to And and Or but things have changed. You should only use And and Or if you specifically want the second expression to be evaluated even if the first fails. In most cases it won't matter, and the miniscule improvement in performance won't be noticed, but there are instances where it is the difference between an unhandled exception or not, e.g.
    VB Code:
    1. If myForm Is Nothing OrElse myForm.IsDisposed Then
    This code checks whether a form exists by testing whether it has never been created or it has been destroyed. If the first condition is True then the second condition is not evaluated, which is desireable. If Or was used instead, the second condition would still be evaluated and a NullReferenceException thrown. The moral of the story is that you should ALWAYS use AndAlso and OrElse unless there is a specific need to use And and Or.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width