If statement with multiple condition

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

C# doesn't allow multiple condition in a single if ? Or did I make a mistake
with my syntax? It keep giving me syntax error when I put "and" or "or" in
the if statemnt for multiple condition checking.

if ((radioAllItems.Checked = TRUE) and
(this.lstSelections.SelectedItems.Count < lstSelections.Items.Count));

Thanks,
Alpha
 
Alpha,

C# uses a different syntax for logical expressions. For and you want to
use &&, for or, you want to use ||, like so:

if ((radioAllItems.Checked = TRUE) &&
(this.lstSelections.SelectedItems.Count < lstSelections.Items.Count))

Hope this helps.
 
Its in the syntax:

AND = &&
OR = ||

equality: == (double equal)

if ((radioAllItems.Checked == TRUE) &&
(this.lstSelections.SelectedItems.Count < lstSelections.Items.Count))
{
// do something
}

(Also no semi-colon at the end of the if statement)
 
Alpha said:
C# doesn't allow multiple condition in a single if ? Or did I make a
mistake
with my syntax? It keep giving me syntax error when I put "and" or "or"
in
the if statemnt for multiple condition checking.

if ((radioAllItems.Checked = TRUE) and
(this.lstSelections.SelectedItems.Count < lstSelections.Items.Count));

In addition to the other answer, make sure you watch out for the common
mistake - almost surely you meant to write

if ((radioAllItems.Checked == true)

not

if ((radioAllItem.Checked = true)
 
Oops! Sorry for such a stupid question. And thanks to all for your help.
The operators are the same as C. I got it. Thanks again.
 
Yes, got that one when I compiled. Thanks.

Jeff Connelly said:
In addition to the other answer, make sure you watch out for the common
mistake - almost surely you meant to write

if ((radioAllItems.Checked == true)

not

if ((radioAllItem.Checked = true)
 
Back
Top