same switch case is used but the definition is different

DEN

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?

Nikola Dimitroff

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

same switch case is used but the definition is different

From Dev

SWITCH-statement two case with same condition and also different condition

From Dev

SWITCH-statement two case with same condition and also different condition

From Dev

Switch with multiple statements for same case

From Dev

If else to switch case with different output

From Dev

The same TFS Build Definition is different for different developers

From Dev

Enum type used in switch case error

From Dev

Android,Use switch case is used to determine the boolean

From Dev

"A case label may only be used within a switch"

From Dev

If and Switch same task, different results?

From Dev

Same name structure with different definition in C

From Dev

What promoted types are used for switch-case expression comparison?

From Dev

What promoted types are used for switch-case expression comparison?

From Dev

Spinner selected string dependent on language used in switch case

From Dev

Json Deserialization with same property name with different case

From Dev

Same object in one case, different object in another?

From Dev

SUM the same field on different CASE conditions

From Dev

Case-insensitive sorted Set - keep same string with different case

From Dev

Can I use the same case value multiple times in a switch

From Dev

How do I do a switch case with saving in the same array?

From Dev

Java - switch case, Multiple cases call the same function

From Dev

Java BufferedReader Switch case same line stored in array

From Java

Can I match on different parts of an expression in a switch case?

From Dev

Jump to a different switch case based on conditon, like a goto

From Dev

using different condition based on column value possible by switch case

From Dev

How to start different activity using switch case in the menu item?

From Dev

ElasticSearch: pro and cons of having different indices for same mapping definition

From Dev

How do I specify a type variable used in an inline type, is the same as a type variable used in a function definition?

From Dev

How do I specify a type variable used in an inline type, is the same as a type variable used in a function definition?

Related Related

  1. 1

    same switch case is used but the definition is different

  2. 2

    SWITCH-statement two case with same condition and also different condition

  3. 3

    SWITCH-statement two case with same condition and also different condition

  4. 4

    Switch with multiple statements for same case

  5. 5

    If else to switch case with different output

  6. 6

    The same TFS Build Definition is different for different developers

  7. 7

    Enum type used in switch case error

  8. 8

    Android,Use switch case is used to determine the boolean

  9. 9

    "A case label may only be used within a switch"

  10. 10

    If and Switch same task, different results?

  11. 11

    Same name structure with different definition in C

  12. 12

    What promoted types are used for switch-case expression comparison?

  13. 13

    What promoted types are used for switch-case expression comparison?

  14. 14

    Spinner selected string dependent on language used in switch case

  15. 15

    Json Deserialization with same property name with different case

  16. 16

    Same object in one case, different object in another?

  17. 17

    SUM the same field on different CASE conditions

  18. 18

    Case-insensitive sorted Set - keep same string with different case

  19. 19

    Can I use the same case value multiple times in a switch

  20. 20

    How do I do a switch case with saving in the same array?

  21. 21

    Java - switch case, Multiple cases call the same function

  22. 22

    Java BufferedReader Switch case same line stored in array

  23. 23

    Can I match on different parts of an expression in a switch case?

  24. 24

    Jump to a different switch case based on conditon, like a goto

  25. 25

    using different condition based on column value possible by switch case

  26. 26

    How to start different activity using switch case in the menu item?

  27. 27

    ElasticSearch: pro and cons of having different indices for same mapping definition

  28. 28

    How do I specify a type variable used in an inline type, is the same as a type variable used in a function definition?

  29. 29

    How do I specify a type variable used in an inline type, is the same as a type variable used in a function definition?

HotTag

Archive