0

I just encountered a weird problem this morning which make me wonder why all of a sudden this type of error occurs, look at this code for instance:

    if(age == 0 || age == 47 || age = 99)
    {
        name = "Those are lucky numbers!";
    }
    else
    {
        name = "Try again...";
    }

The error is: Error CS0019: Operator '||' cannot be applied to operands of type 'bool' and 'int'.

What the...? Hehe... I means type 'bool' and 'int' are the most frequent types we'll want to use with this kind of operations. Why wouldn't they work?

Furthermore, I'm sure I've done this thousands of time before, I don't understand why today it occurs... Or maybe I did this with the Ruby language. Still I truly have the feelings it worked before with C#.

Anyone have a logical explanation? Something I'm missing today? :D

djechlin
  • 59,258
  • 35
  • 162
  • 290
TheScholar
  • 2,527
  • 5
  • 23
  • 25
  • 2
    Here's your problem: `age = 99` – kei Dec 09 '13 at 18:29
  • 4
    This question appears to be off-topic because the problem was just a syntax error. – Zong Dec 09 '13 at 18:29
  • you have '=' rather than '==' in 'age = 99'... – digiliooo Dec 09 '13 at 18:29
  • 4
    You're lucky it's not C++ or you wouldn't even know what happened. – S_F Dec 09 '13 at 18:30
  • @ZongZhengLi why are syntax errors off topic? – djechlin Dec 09 '13 at 18:31
  • as everyone said, age=99 is the problem. The reason is because you assign age to 99 and then 99 (an int) becomes the right hand operand. The left hand operand is a bool type and there is no defined || operator which works with left side bool, right side int. – doog abides Dec 09 '13 at 18:31
  • Your issue is not with the logical operator. It is the lack of logical operator. – Floris Dec 09 '13 at 18:31
  • Damn, I MUST be very tired this morning!!! haha I can't believe I didn't see it. – TheScholar Dec 09 '13 at 18:32
  • @djechlin They are unlikely to help future visitors (at least in the way this question is phrased), and it seems to be the general concensus: http://meta.stackexchange.com/questions/160275/are-questions-about-syntax-errors-too-localized – Zong Dec 09 '13 at 18:33
  • @ZongZhengLi that's obsolete... pre- close vote overhaul. – djechlin Dec 09 '13 at 18:36
  • @ZongZhengLi problem is it's obviously a duplicate of zillions of question. I made the title more precise, googled it and got this http://stackoverflow.com/questions/4007493/cs0019-operator-cannot-be-applied-to-operands-of-type-bool-and-int – djechlin Dec 09 '13 at 18:37
  • @TheScholar in the future please google your error message, this would show you your problem. Downvoted since this research was missing. – djechlin Dec 09 '13 at 18:37

4 Answers4

8

Your last age needs a second = to do the comparison

 if(age == 0 || age == 47 || age == 99)

you had age = 99

Pierre-Luc Pineault
  • 8,993
  • 6
  • 40
  • 55
4

The last component of your condition, age = 99, evaluates to an int, not a bool, because you used the assignment operator, =, instead of the equality operator, ==.

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
2

I means type 'bool' and 'int' are the most frequent type we'll want to perform this type of operations

Nope.

That error is saying that you can't write something like true || 42.
This doesn't make any sense.

Your actual problem is that age = 9 is assignment, not comparison.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

You mean age == 99, not age = 9.

age = 9 is an expression that evaluates to 9, with a side-effect of setting the variable age to 9. Therefore you can do something like a = (age = 99), and a (if it is declared as int already) will be set to 99.

That's why your error message is complaining about an int being passed to ||.

One debugging strategy would be to write this out on multiple lines:

if( a ||
    b ||
    c)

which would isolate the operator your error is complaining about. You might have seen your error in this case.

djechlin
  • 59,258
  • 35
  • 162
  • 290