if a = 1 And b = 2 then ... equivalent syntax for C# ?

  • Thread starter Thread starter Rich P
  • Start date Start date
R

Rich P

I tried

if (a == 1 && b == 2)
{
...
}

but got an error saying && can't be used with ints. How do I write the
above statement in C#?

Thanks,

Rich
 
Rich P said:
I tried

if (a == 1 && b == 2)
{
..
}

but got an error saying && can't be used with ints. How do I write the
above statement in C#?

Thanks,

Rich

Do it like this, and you'll succeed

if ((a == 1) && (b == 2))
{

}

regards

Finn
--
Der er 10 slags mennesker - Dem som forstår binær og dem som ikke gør.
There are 10 kinds of people. Those who understand binary and those who
don't.
Es gibt 10 Arten von Menschen. Die, die Binär verstehen, bzw. die, die es
nicht tuhen.
 
Nevermind. I also used a single & which also was not working on one of
my if's - turned out I had a typo. Problem has been fixed using single
&.

Rich
 
Rich said:
I tried

if (a == 1 && b == 2)
{
..
}

but got an error saying && can't be used with ints. How do I write the
above statement in C#?

Thanks,

Rich

The code that you show does not give that error.

You can get that error if you mistyped one of the comparisons as an
assignment:

if (a = 1 && b == 2) {

The value of an assignment is the value that you assigned to the
variable, so it's equivalent of:

a = 1;
if (1 && b == 2) {
 
if (a = 1 && b == 2) {

I must have done that. Thanks all for the replies. Problem solved.

Rich
 
I must have done that. Thanks all for the replies. Problem solved.

Clearly you didn't post your ACTUAL code. Please, please, PLEASE, in the
future, always post your actual code. Do NOT re-type it in your post. Typos
are often corrected that way.
 
Back
Top