3

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
        }
Wayne Koorts
  • 10,861
  • 13
  • 46
  • 72
cyberbobcat
  • 1,169
  • 1
  • 18
  • 34

7 Answers7

7

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
  • 32,260
  • 12
  • 84
  • 119
3

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

Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
  • I'm guessing he still wants to avoid that duplication of having to call the method in every case – Garry Shutler Feb 20 '09 at 10:45
  • 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 Feb 20 '09 at 13:51
  • 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 Feb 20 '09 at 14:44
3

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
  • 9,461
  • 6
  • 55
  • 66
2

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
  • 2,147
  • 2
  • 17
  • 19
2

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
  • 17,058
  • 3
  • 60
  • 75
1

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
  • 106,783
  • 21
  • 203
  • 265
0

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
  • 12,643
  • 13
  • 53
  • 60