Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent second half of an if statement when first half is null

I have a statement in VB.net that I thought I wrote correctly to prevent the second half from being evaluated. It looks like this:

If ((myDataSet2 IsNot Nothing) Or myDataSet2.Tables("CurData").Rows.Count > 0)

However it does not skip the second expresion "myDataSet2.Tables("CurData").Rows.Count > 0" like I want it to.

What should I change?

like image 887
George Mikan Avatar asked Jul 09 '13 15:07

George Mikan


2 Answers

Use the OrElse operator.

If myDataSet2 IsNot Nothing OrElse myDataSet2.Tables("CurData").Rows.Count > 0

EDIT: See my comment on your original question. You are PROBABLY looking for:

If myDataSet2 IsNot Nothing AndAlso myDataSet2.Tables("CurData").Rows.Count > 0

That will check if myDataSet2 isn't null. Assuming that it's not, it will then check that there is at least one row. If it is null, then the second condition won't be checked.

like image 56
Douglas Barbin Avatar answered Sep 22 '22 19:09

Douglas Barbin


You need to put the second statement into the first if-clause.

Like this:

If(statement1) then
   If(statemtent2) then
   Else
   End if
Else
End If

As it is now both are evaluated and if one of them is true the content in your if-clause will be executed.

like image 44
user2425234 Avatar answered Sep 23 '22 19:09

user2425234