Switch ?

  • Thread starter Thread starter John S
  • Start date Start date
J

John S

In my case statements I need to evaluate a number but it doesn't like
my syntax.
Example:
x= 45
Switch (x){
case > 7 < 10:
<code>
break;
case > 11 < 50
<code>
break;
default:
break;

}

It does not like the < or > than comparison operators. I know there
has to be a way to do this.
 
You could always use an if statement:

x=45;
if (x > 7 && x < 10)
<code>
else if (x > 11 && x < 50)
<code>
else
<default>
 
John S said:
In my case statements I need to evaluate a number but it doesn't like
my syntax.
Example:
x= 45
Switch (x){
case > 7 < 10:
<code>
break;
case > 11 < 50
<code>
break;
default:
break;

}

It does not like the < or > than comparison operators. I know there
has to be a way to do this.


Sorry, can't do it with c#. Only finite values can be used. You can stack
cases but no ranges.

If you search on "switch-statement ???", you will find a decent post
explaining this.

Dwight
 
John S said:
In my case statements I need to evaluate a number but it doesn't like
my syntax.
Example:
x= 45
Switch (x){
case > 7 < 10:
<code>
break;
case > 11 < 50
<code>
break;
default:
break;

}

It does not like the < or > than comparison operators. I know there
has to be a way to do this.

Yes, with "if":

if (x > 7 && x < 10)
{
....
}
else if (x > 11 && x < 50)
{
....
}

switch/case is for specific values, not ranges.
 
I would also suggest picking up a C# syntax book and learn it rather than
inventing your own. saves yourself a lot of aggravation in the long run.
 
Back
Top