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?
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.
Comments