Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Evaluation of And in an If statement

  1. #1

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Resolved [RESOLVED] [2005] Evaluation of And in an If statement

    I noticed that if an If .. End If statement contains multiple conditions that need to be True for it continue, all conditions seem to be evaluated even if the first one does not return True. An example:
    vb Code:
    1. If DataGridView2.Rows.Count > 0 And DataGridView2.Rows(0).Cells("available") IsNot Nothing Then
    2.     ....
    3. End If
    This still bails due to the 2nd part, as there are no rows yet in this DatGridView. Is there a better way to deal with this, other than a nested If statement, because that's just ugly. In some other programming languages, the second part is never evaluated if the first one fails. Is this just one of those little idiosyncrasies of VB?

    Gr,
    Mightor
    What the world needs is more geniuses with humility, there are so few of us left.
    If my post was accidentally useful, please rate it as such, thanks!
    Mon aéroglisseur est plein des anguilles.

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] Evaluation of And in an If statement

    You can use AndAlso.

    Code:
    If DataGridView2.Rows.Count > 0 AndAlso _
       DataGridView2.Rows(0).Cells("available") IsNot Nothing Then _
       ....End If
    There is an OrElse for OR statements too.

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Evaluation of And in an If statement

    If you use AndAlso, it will stop evaluating the conditions if one of them returns False.

    VB Code:
    1. If DataGridView2.Rows.Count > 0 AndAlso DataGridView2.Rows(0).Cells("available") IsNot Nothing Then
    2.     ....
    3. End If
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] Evaluation of And in an If statement

    Ah thanks people! That did the trick.

    Gr,
    Mightor
    What the world needs is more geniuses with humility, there are so few of us left.
    If my post was accidentally useful, please rate it as such, thanks!
    Mon aéroglisseur est plein des anguilles.

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