I have an enum, which I would like to call a function on to change its value.
The enum is called "Direction" (North/South/East/West), and I want to make an extension method called "TurnLeft()", which will change the value of the enum itself.
Example:
Direction l_direction = Direction.N;
l_direction.TurnLeft() // value should become Direction.W
Unfortunately it seems that the standard way of implementing an extension method can not change the value of the enum itself. I had hoped that the "this" keyword could have done it, believing it would implicitly work like "ref", but no. And it is not possible to use both "this" and "ref" for the same argument.
Is there any way to accomplish this?
This is what I have:
public enum Direction
{
N = 0,
E,
S,
W,
}
public static class DirectionFunction
{
public static void TurnLeft(this Direction a_direction)
{
switch(a_direction)
{
case Direction.N:
a_direction = Direction.W;
return;
case Direction.W:
a_direction = Direction.S;
return;
case Direction.S:
a_direction = Direction.E;
return;
case Direction.E:
a_direction = Direction.N;
return;
}
}
}