pvphuc

Hi everybody!
In VB I can use Select keyword to do following command:

Select case test
case is < 100, is>=500,444
Do St
end Select

Can C# do the same things like the above example ( just use 1 case)

pcphuc.







Re: Visual C# General Can Switch in C# do multi-comparison like VB on 1 "case" row?

boban.s

For this exact example you should use if .. else if .. else if .. else. But yes C# has same coditional execution control with some minor sintax and functional differences:

int i = 100;
switch (i)
{
case 100:

case 200:

// do something when i is 100 OR 200
break;
case 300:
//do something else when i is 300
break;
default:

//do something when neither case is executed
break;
}






Re: Visual C# General Can Switch in C# do multi-comparison like VB on 1 "case" row?

Thomas Danecker

The only way to achieve the same as in your example is to use if...else if...else.

But i just want to add to the switch case construct in C# that it is more limitting than C++'s version of switch case:

Code Snippet

int i = 100;

switch (i)
{
case 100:

// you can't do something here without a break before the next case.

case 200:

// do something when i is 100 OR 200
break;
case 300:
//do something else when i is 300
break;
default:

//do something when neither case is executed
break;
}

EDIT:

the only construct you can use for your approach would be:

Code Snippet

if(test < 100 || test >= 500 || test == 444)

{

// do something

}






Re: Visual C# General Can Switch in C# do multi-comparison like VB on 1 "case" row?

pvphuc

Thanks for your response !

Have a nice day.