3

What is the best way to reuse switch logic. I have this switch statement that keeps appearing in my code. Rather then copy paste it I would like to make a function that calls other delegates and pass these delegates in as parameters.

Or is there a better way?

Function 1:

switch (test)
        {
            case "x":
                DoSomethingX();
                break;
            case "y":
                DoSomethingY();
                break;
            case "z":
                DoSomethingZ();
                break;
        }

Function 2:

switch (test)
    {
        case "x":
            DoSomethingXxxx();
            break;
        case "y":
            DoSomethingYyyy();
            break;
        case "z":
            DoSomethingZyyy();
            break;
    }
Xander
  • 9,069
  • 14
  • 70
  • 129
  • You need to provide more details for us to help. Which parts of the switch are invariant? Only the cases themselves, or the actions as well? What about the variable you're switching on, is that the same or does it vary? – Nick Lewis Jul 14 '09 at 17:34
  • It kind of sounds like he's saying the cases are invariant, but the actions change... but yeah, we need more details. – Dave Jul 14 '09 at 17:35
  • the cases stay the same the actions change – Xander Jul 14 '09 at 19:02

3 Answers3

9

You could also have a Dictionary (or Func instead of Action) or something like that (considering your functions have a similar signature). Then you could instead of using a switch, you could have something like:

public class MyClass
{
    Dictionary<string, Action> myDictionary;

    public MyClass()
    {
        BuildMyDictionary();
    }

    private Dictionary<int, Action<int, int>> BuildMyDictionary()
    {
        myDictionary.Add("x", DoSomethingX);
        myDictionary.Add("y", DoSomethingY);
        myDictionary.Add("z", DoSomethingZ);
        myDictionary.Add("w", DoSomethingW);
    }


    public void DoStuff()
    {
        string whatever = "x"; //Get it from wherever
        //instead of switch
        myDictionary[t]();
    }
}

I answered a similar question here with a similar example.

Also, try using enums instead of strings in your switch statement.

Community
  • 1
  • 1
Samuel Carrijo
  • 17,449
  • 12
  • 49
  • 59
4

See if you can refactor this using an interface and different implementations of the interface.

public interface Test {
    void DoSomething();
}

public class TestX : Test {
    void DoSomething() {
    }
}

public class TestY : Test {
    void DoSomething() {
    }
}

public class TestZ : Test {
    void DoSomething() {
    }
}


void func(Test test) {
    test.DoSomething();
}
Jared Oberhaus
  • 14,547
  • 4
  • 56
  • 55
0

As I try to understand your question, I might go to following:

public enum Test{
    X, Y, Z
}

/**
* test function call 
* @a_Test - enumeration class for Test
*/
public void test(Test a_Test){
 switch(a_Test){
   case X:
       x();
       break;
   case Y:
       y();
       break;
   case Z:
       z();
       break;
 }//switch
}//test

I hope it helps.

Tiger

Tiger
  • 507
  • 1
  • 6
  • 16