switch - Process Multiple Cases Same Way.

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
 
T

Tom Porterfield

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
 
D

Drebin

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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top