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;
    }
3
  • 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? Commented Jul 14, 2009 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. Commented Jul 14, 2009 at 17:35
  • the cases stay the same the actions change Commented Jul 14, 2009 at 19:02

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Sorry @Svish, I'm sure you'll beat me next time. It's happened before. :)
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.