switch case multiple choice...  
Author Message
Lug Vélez Schmitz





PostPosted: Visual C# Language, switch case multiple choice... Top

Hi everyone...

I have a very simple question, but I can’t find the answer to give a solution... Few centuries ago in BASIC language, you canned do something like that:

Select Case SomethingtoCheck
Case 21, 24, 25, 27 To 32, 34, 35, 38
SomethingToDo...


As you can see you canned choice multiples value cases, but this simple thing I can’t find a translation to C#... (probably is the most fool question ever asked)

Thanks...



Visual C#11  
 
 
n0n4m3





PostPosted: Visual C# Language, switch case multiple choice... Top

Hi,
you can do it like this:

switch(somethingToCheck)
{
case 21:
case 24:
SomethingToDo();
break;
case 30:
case 50:
SomethingToDo2();
break;
default:
break;
}


You can't do things like a "fall-through", that is:

switch(somethingToCheck)

{
case 21:
SomethingToDo();
case 24:
SomethingToDo2();
break;
default:
break;
}

although there's an "hack" for this that is to use a "goto case 24;" afther the SomethingToDo(). Ranges like 27 to 32 are not allowed either, you'd have to use an if statement for that.