I already searched on how to do a switch on the value instead of the enumeration key in C# but with no result, all the posts I've found says that we don't need to use the value, we can just use the key.
But in my case, I want to use the value because I made an enumeration with multiple keys sharing the same value, here's the code :
public enum PlayerPosition {
North = 0,
Top = 0,
South = 1,
Bottom = 1,
East = 2,
Right = 2,
West = 3,
Left = 3
}
switch (obj.PlayerPosition)
{
case PlayerPosition.North:
// some code
break;
case PlayerPosition.South:
// some code
break;
case PlayerPosition.East:
// some code
break;
case PlayerPosition.West:
// some code
break;
default:
throw new ArgumentOutOfRangeException();
}
I think this is working but I don't find it really readable...
What I would like to achieve is something like this :
switch (obj.PlayerPosition)
{
case PlayerPosition.Top:
case PlayerPosition.North:
// some code
break;
case PlayerPosition.Bottom:
case PlayerPosition.South:
// some code
break;
case PlayerPosition.Right:
case PlayerPosition.East:
// some code
break;
case PlayerPosition.Left:
case PlayerPosition.West:
// some code
break;
default:
throw new ArgumentOutOfRangeException();
}
The example above doesn't work because it is a duplicated case label. How could I achieve that ?
EDIT : In this enumeration North/Top, South/Bottom are exactly the same, they just represent the position of the player around a table with 4 chairs. But we have old configuration files who use North/South/East/West and new configuration files who are using Top/Bottom/Right/Left.