5

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?

George Mikan
  • 135
  • 7
  • I said to use OrElse in my answer, but you are probably wanting to do a short-circuit logical AND, right? In that case, use AndAlso. I ask because it doesn't make a lot of sense to check if your dataset isnot nothing, then if that's false (meaning your dataset is nothing), checking some property of the non-existent dataset. You will get a null reference that way. – Douglas Barbin Jul 09 '13 at 15:29
  • Yes you are correct, what I really want to do is `AndAlso`! – George Mikan Jul 09 '13 at 15:35

2 Answers2

8

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.

Douglas Barbin
  • 3,595
  • 2
  • 14
  • 34
3

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.

user2425234
  • 67
  • 1
  • 10