Home All Groups Group Topic Archive Search About

If statement with multiple condition

Author
4 Apr 2005 7:01 PM
Alpha
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

Author
4 Apr 2005 7:16 PM
Marcie Jones
In C#, you use && for and, and || for or.  Also != is not equal to.

Marcie

On Mon, 4 Apr 2005 12:01:02 -0700, "Alpha"
<Al***@discussions.microsoft.com> wrote:

Show quoteHide quote
>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
Are all your drivers up to date? click for free checkup

Author
4 Apr 2005 7:31 PM
Alpha
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.

Show quoteHide quote
"Marcie Jones" wrote:

> In C#, you use && for and, and || for or.  Also != is not equal to.
>
> Marcie
>
> On Mon, 4 Apr 2005 12:01:02 -0700, "Alpha"
> <Al***@discussions.microsoft.com> wrote:
>
> >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
>
>
Author
4 Apr 2005 7:17 PM
Nicholas Paldino [.NET/C# MVP]
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.


--
               - Nicholas Paldino [.NET/C# MVP]
               - mvp@spam.guard.caspershouse.com

Show quoteHide quote
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:0DBC7AA0-33C2-4D13-A0D5-F7BF3A8D9DBE@microsoft.com...
> 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
Author
4 Apr 2005 7:17 PM
Alex Passos
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)

Show quoteHide quote
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:0DBC7AA0-33C2-4D13-A0D5-F7BF3A8D9DBE@microsoft.com...
> 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
Author
4 Apr 2005 7:20 PM
Jeff Connelly
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:0DBC7AA0-33C2-4D13-A0D5-F7BF3A8D9DBE@microsoft.com...
> 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)
Author
4 Apr 2005 7:39 PM
Joanna Carter (TeamB)
"Jeff Connelly" <nom***@thank.you> a écrit dans le message de news:
emWFRuUOFHA.3***@TK2MSFTNGP10.phx.gbl...

> if ((radioAllItems.Checked == true)

or even :

if ((radioAllItems.Checked) ...

Joanna

--
Joanna Carter
Consultant Software Engineer
Author
4 Apr 2005 7:49 PM
Alpha
Yes, thank you.  That's slick.

Show quoteHide quote
"Joanna Carter (TeamB)" wrote:

> "Jeff Connelly" <nom***@thank.you> a écrit dans le message de news:
> emWFRuUOFHA.3***@TK2MSFTNGP10.phx.gbl...
>
> > if ((radioAllItems.Checked == true)
>
> or even :
>
> if ((radioAllItems.Checked) ...
>
> Joanna
>
> --
> Joanna Carter
> Consultant Software Engineer
>
>
>
Author
4 Apr 2005 9:37 PM
Jeff Connelly
"Joanna Carter (TeamB)" <joannac@nospamforme.com> wrote in message
news:OAsQY4UOFHA.3156@TK2MSFTNGP15.phx.gbl...
> "Jeff Connelly" <nom***@thank.you> a écrit dans le message de news:
> emWFRuUOFHA.3***@TK2MSFTNGP10.phx.gbl...
>
>> if ((radioAllItems.Checked == true)
>
> or even :
>
> if ((radioAllItems.Checked) ...

Yes, if the thing you're testing is actually a boolean value, that's better.
Author
4 Apr 2005 7:43 PM
Alpha
Yes, got that one when I compiled.  Thanks.

Show quoteHide quote
"Jeff Connelly" wrote:

>
> "Alpha" <Al***@discussions.microsoft.com> wrote in message
> news:0DBC7AA0-33C2-4D13-A0D5-F7BF3A8D9DBE@microsoft.com...
> > 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)
>
>
>

Bookmark and Share