switch - Process Multiple Cases Same Way.

  • Thread starter Thread starter Guadala Harry
  • Start date Start date
G

Guadala Harry

After RTFM, I hope I have missed something: I would like to have a switch()
statement process multiple input values the same way - but without having
multiple case: blocks for multiple input values that need to be processed
the same way. For example, please consider the following sample switch block
and notice that "other value 1, 2, and 3" all execute DoOtherThing(). How
can I get all of those [case "other value x"] cases to NOT have to appear in
their own case block?

switch (someString) {
case "one value":
DoOneThing();
break;
case "other value 1":
DoOtherThing();
break;
case "other value 2":
DoOtherThing();
break;
case "other value 3":
DoOtherThing();
break;
case "other value 4":
DoOtherThing();
break;
default:
GiveUp();
break;
} // end switch


Thanks!

-G
 
Guadala said:
After RTFM, I hope I have missed something: I would like to have a switch()
statement process multiple input values the same way - but without having
multiple case: blocks for multiple input values that need to be processed
the same way. For example, please consider the following sample switch block
and notice that "other value 1, 2, and 3" all execute DoOtherThing(). How
can I get all of those [case "other value x"] cases to NOT have to appear in
their own case block?

switch (someString) {
case "one value":
DoOneThing();
break;
case "other value 1":
DoOtherThing();
break;
case "other value 2":
DoOtherThing();
break;
case "other value 3":
DoOtherThing();
break;
case "other value 4":
DoOtherThing();
break;
default:
GiveUp();
break;
} // end switch

Stack them.

switch (someString) {
case "one value":
DoOneThing();
break;
case "other value 1":
case "other value 2":
case "other value 3":
case "other value 4":
DoOtherThing();
break;
default:
GiveUp();
break;
} // end switch
 
Here ya go:

int intTest = 4;
switch ( intTest )
{
case 0: case 1: case 2: case 3:
{
// Do something for 0 through 3
break;
}
case 4:
{
MessageBox.Show("Hey!");
break;
}
}
 
Back
Top