-5

Not sure what I am missing here.. Should be pretty simple..

tblCurrent does NOT equal NULL tblCurrent.Rows.Count DOES equal 0

if (tblCurrent != null | tblCurrent.Rows.Count != 0)
{
    //Do something
}
else
{
    // This is what I want
}

It should see that the right condition is 0 so it should return false and put in the else block? What am I missing??

Ron
  • 277
  • 1
  • 2
  • 13
  • If `tblCurrent` is not null then `tblCurrent != null` is true and it would not go to the else block. – juharr Sep 05 '13 at 18:30
  • If `tblCurrent != null`, that condition will always evaluate to `true`. Are you sure you don't want to use AND (`&&`)? Also, you should be using `||` instead of `|`. – JosephHirn Sep 05 '13 at 18:30
  • 1
    Think about it -- you're asking it to check if `tblCurrent` is not null, and since `tblCurrent` is not null, guess how it evaluates? – LittleBobbyTables - Au Revoir Sep 05 '13 at 18:30
  • 1
    Please investigate a little bit before posting questions. You are using the wrong operator. – user823959 Sep 05 '13 at 18:32
  • Yes but I also have the tblCurrent.Rows.Count != 0 condition. If either one of these conditions evaluate to false I want to be in the else block. And the rows.count does evaluate to false. I thought that a single pipe | checked both conditions – Ron Sep 05 '13 at 18:32
  • 1
    @Ron, a [bitwise or (|)](http://msdn.microsoft.com/en-us/library/kxszd0kx.aspx) is way different than a [conditional or (||)](http://msdn.microsoft.com/en-us/library/6373h346.aspx). – gunr2171 Sep 05 '13 at 18:39
  • What's with all these votes to close? Although this is definitely not a hard question, but it's not a "check the manual" or "gimmecodez" question either! I don't think it should be closed for "no effort". – Sergey Kalinichenko Sep 05 '13 at 18:42
  • @LittleBobbyTables Even the straight boolean logic could be hard to someone with no prior exposure to it. My personal criterion of sufficient "minimal understanding" is posting relevant code that compiles, which OP did :-) – Sergey Kalinichenko Sep 05 '13 at 18:54
  • To clarify I got that information from here http://stackoverflow.com/questions/1746302/c-sharp-or-operator which was the top rated answer so I figured that was my problem. I should looked further in the comments section - Thank you dasblinkenlight for the clarification. – Ron Sep 05 '13 at 21:46

2 Answers2

5

If tblCurrent does not equal null, then tblCurrent != null evaluates to true, so the overall OR would evaluate to true as well, because an OR evaluates to true if, and only if, one or both of its sides evaluate to true.

It looks like your logic should have used the AND operator && instead of an OR, like this:

if (tblCurrent != null && tblCurrent.Rows.Count != 0) {
    ...
} else {
    ...
}

The && operator short-circuits the evaluation, so you would not get an exception even when the tblCurrent is null.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • The && operator short-circuits the evaluation, so you would not get an exception even when the tblCurrent is null. I think this may be where I was getting lost because there will be times that the table is actually null and I dont want to also be checking the rows equal 0 on a null table – Ron Sep 05 '13 at 18:33
  • 1
    @Ron The `&&` operator checks the left side first, and sees if it can decide the overall result based on the left side alone. Since `AND` evaluates to `false` when either side is false, the `&&` knows the answer to `false && anythingElse` without knowing what that `anythingElse` might be. In your case, when `tblCurrent` is `null`, `tblCurrent != null` would evaluate to `false`, so `&&` would not go any further, and return `false`. The `tblCurrent.Rows` expression would not get evaluated. The official name of that trick is short-circuiting. – Sergey Kalinichenko Sep 05 '13 at 18:37
3

The correct OR operator is ||. The | operator is a bitwise OR.

Your logic requires AND, not OR.

if (tblCurrent != null && tblCurrent.Rows.Count != 0)
Itay
  • 16,601
  • 2
  • 51
  • 72
  • 1
    It's not actually the bitwise OR in context, because both operands are boolean. It's just a non-short circuiting OR. If the operands were numeric then it would be a bitwise OR. – Servy Sep 05 '13 at 18:43