Is there a way that I can execute the same line of code for each "Case" but only have to type it in once instead of having the same code specified for all Cases ?
switch (SomeTest)
{
case "test1":
{
// Do something for test 1
break;
}
case "test2":
{
// Do something for test 2
break;
}
case "test3":
{
// Do something for test 3
break;
}
// =====> Then do something generic here for example if case is test1, test2 or test3
}
Are you possibly over thinking it?
switch(SomeTest)
{
// specific stuff
}
// code you want running for every case
Otherwise the best you can do without setting a flag or something is:
switch(SomeTest)
{
// specific stuff
}
switch(SomeTest)
{
case "Test1", "Test2", "Test3":
// stuff for the matching cases
}
Or if you want to run the code for every case you match:
bool runGenericStuff = true;
switch(SomeTest)
{
// specific stuff
default:
runGenericStuff = false;
}
if (runGenericStuff)
{
// run generic stuff
}
That saves you having to set the flag in every case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With