views:

202

answers:

7

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
        }
+1  A: 

C#'s switch does not have fall through to the next case by default (unlike C/C++), but you can goto another case.

Debug.Assert(value != 99);
switch (value) {
  case 1:
    DoSomething();
    goto case 99;
  case 2:
    DoSomethingElse():
    goto case 99:
  case 3:
    DoNothingHere();
    break;

  case 99:
    // A case that will never be directly called.
    DoSomethingInCommon();
    break;
}
Richard
Goto! aaah the devil! *runs away screaming* (Looks like this is one of the cases where goto can be put to good use though.)
RSlaughter
Remember, it is "*overuse* of goto considered harmful"
Richard
I think 98 cases of goto would easily count as overuse...
dbkk
99 as chosen as being clearly distinct (will amend to make this clearer).
Richard
+7  A: 

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.

Garry Shutler
Yes that sounds like a good idea - thanks.
cyberbobcat
+3  A: 

Put the common logic in a seperate method and call it on each case label that requires it.

Gerrie Schenck
I'm guessing he still wants to avoid that duplication of having to call the method in every case
Garry Shutler
As a general rule, I don't consider calling the same method multiple times (when there's no common surrounding context) to be duplication. Doing anything in code will always take at least one line.
Greg D
Imagine instead of calling Method1 for each match you now want to call Method2. If you've called it from every case, you now have to change every case. Therefore, to me there is duplication even though your code is well factored.
Garry Shutler
+2  A: 

bool ShouldIDoSomething = false;

    switch (SomeTest)
    {
        case "test1":
            {
                // Do something for test 1 
                ShouldIDoSomething=true;
                break;
            }
        case "test2":
            {
                // Do something for test 2 
                ShouldIDoSomething=true;
                break;
            }
        case "test3":
            {
                // Do something for test 3 
                ShouldIDoSomething=true;
                break;
            }
        // =====> Then do something generic here for example if case is test1, test2 or test3

    }

if(ShouldIDoSomething) DoSomething generic

Khadaji
I'm guessing he still wants to avoid that duplication of having to set the flag in every case
Garry Shutler
+2  A: 

There's no special syntactic support for this. You could get the effect by doing something like this:

public static void DoSomething(string testValue)
{
    bool hasMatch = true;
    switch(testValue)
    {
        case "Test1":
            WL("Test1");
            break;
        case "Test2":
            WL("Test2");
            break;
        case "Test3":
            WL("Test3");
            break;
        default:
            WL("No match.");
            hasMatch = false;
            break;
    }
    if (hasMatch)
    {
        WL("Match found.");
    }
}
Weeble
+1  A: 

I would do it this way:

    bool doSomething = true;

    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;
            }
        default:
            {
            doSomething = false;
            }
    }

    if (doSomething)
    {
    // your code here
    }
Juozas Kontvainis
A: 

Slightly simpler

switch(x) {
  case Foo:
    // your normal cases here
    break;
  default: 
    // post-processing NOT covered by normal cases
    return;  
}
// post-processing if any of the cases were handled
dbkk