Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If (something) or (somethingElse) problem 2

Status
Not open for further replies.

wokkie

Programmer
May 9, 2003
52
GB
Hi,

I am having trouble using OR in an If loop. I have

if (cboCeaseType.SelectedValue.ToString () == "2") or (cboCeaseType.SelectedValue.ToString () == "5" ){
do something
} else {

do something else
}

It doesn't like this. How do I have an OR in an If loop? What should the above be?

Cheers

Ian
 
c# is more java-like than vb-like and uses java-like operators such as && (VB 'And') and || (VB 'Or').


Rhys
"When one burns one's bridges, what a very nice fire it makes" -- Dylan Thomas
"As to marriage or celibacy, let a man take the course he will. He will be sure to repent" -- Socrates
 
Thanks, I have being using VBA for years and often get mixed up!

Thanks again
 
You'll also need to add an additional set of parenthesis, since the if statement wants them around the whole expression:
Code:
if ((cboCeaseType.SelectedValue.ToString() == "2") ||
    (cboCeaseType.SelectedValue.ToString() == "5"))
{
Another suggestion would be to put the constant part of your comparison on the left side of the comparison operator. That way if you leave one of the equals signs off, you'll get a "Can't assign value to constant" error. Looks a little odd, but works because the comparison operator is reflexive.
Code:
if (("2" == cboCeaseType.SelectedValue.ToString()) ||
    ("5" == cboCeaseType.SelectedValue.ToString()))
{
Chip H.



If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top