[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:
If DataGridView2.Rows.Count > 0 And DataGridView2.Rows(0).Cells("available") IsNot Nothing Then
....
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
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.
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:
If DataGridView2.Rows.Count > 0 AndAlso DataGridView2.Rows(0).Cells("available") IsNot Nothing Then
....
End If
Re: [2005] Evaluation of And in an If statement
Ah thanks people! That did the trick.
Gr,
Mightor