2

I have to use the same switch case repeatedly in different functions. In different functions, the switch case definition will be different. For example:

    int groupID = somenumber;
    private void FunctionA()
    {
        switch (groupID)
        {
            case 0:
                // Do Action A
                break;
            case 1:
                // Do Action B
                break;
            case 2:
                // Do Action C
                break;
        }
    }

    private void FunctionB()
    {
        switch (groupID)
        {
            case 0:
                // Do Action Z
                break;
            case 1:
                // Do Action X
                break;
            case 2:
                // Do Action Y
                break;
        }
    }

Is that any method to use the same switch case once time but the definition can be different?

DEN
  • 1,893
  • 7
  • 27
  • 51
  • 3
    The best approach for these types of situations is to use polymorphism and inheritance. Have you considered that approach? – Gayot Fow Sep 25 '13 at 07:47
  • 1
    polymorphism and inheritance on this? how? – DEN Sep 25 '13 at 07:49
  • 1
    @DEN: The name of that refactoring is pretty straightforward: [Replace conditional with polymorphism](http://sourcemaking.com/refactoring/replace-conditional-with-polymorphism). – vgru Sep 25 '13 at 09:14

3 Answers3

6

I sincerely believe there's a better way of doing what you want to accomplish but you need to give us more details.

In the current state of affairs one possible solution is:

private void SwitchStuff(Action action0, Action action1, Action action2)
{
    switch (groupID)
    {
        case 0:
            action0();
            break;
        case 1:
            action1();
            break;
        case 2:
            action2();
            break;
    }
}

And use it like:

SwitchStuff(DoActionA, DoActionB, DoActionC);
SwitchStuff(DoActionX, DoActionY, DoActionZ);

Where those actions are lambdas/normal methods.

Nikola Dimitroff
  • 6,127
  • 2
  • 25
  • 31
5

One possibility is to use one method that uses the Action delegate like this:

private void MyFunction(int groupID, Action a, Action b, Action c)
{
    switch (groupID)
    {
        case 0:
            a();
            break;
        case 1:
            b();
            break;
        case 2:
            c();
            break;
    }
}

Then you can call it like this:

MyFunction( groupId, A, B, C );

or

MyFunction( groupId, Z, X, Y );

EDIT

Or a better way is a variant of what Matthew Watson suggested:

Change the method to

private void MyFunction(int groupID, params Action[] actions)
{
  actions[groupId]();
}

and get rid of the entire switch case. You call it in exactly the same way as above.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
4

You can replace switch statements with polymorphism. Create interface or base class which represents your groups:

public interface Group
{
    void FunctionA(); // if Group is a class, then make these methods
    void FunctionB(); // virtual or abstract
}

Then create classes for each group (sorry, I don't know what 0,1,2 could mean here, so consider better descriptive names):

public class Group0 : Group
{
   public void FunctionA()
   {
      // Do Action A
   }

   public void FunctionB()
   {
      // Do Action Z
   }
}

Then you create appropriate instance of group:

private IGroup CreateGroup(int groupID)
{
    switch (groupID)
    {
        case 0: return new Group0();
        case 1: return new Group1();
        case 2: return new Group2();
        default: 
           throw new ArgumentException();
    }
}

And use this instance of group in your application (application do not know which implementation of group it is using):

IGroup group = CreateGroup(somenumber);
group.FunctionA(); // if group is Group1 then do action B
group.FunctionB(); // if group is Group1 then do action X
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459