C# Enum extension function - impossible to set value?

Started by
1 comment, last by QNAN 8 years ago

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;
		}
	}
}
Advertisement

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?


Not really :(. You see in case of value types (like struct or enum) member methods have an actual reference to the value so this is actually a "ref this" so to say. That way member methods of value types have the ability to mutate the original value, which can be useful in some cases but is actually a bit dangerous and ill-advised to do so (even the language designers themselves have the same opinion), since: the compiler will allow struct methods to be invoked on read-only structures too, but pass this by value, because in most use cases you only work with copies of a value type (even in case of structs and enums), e.g.: when iterating on a enumerable of structs you actually receive a copy, or when retrieving the value of a struct property, the same way you only get a copy of the struct value (that is one reason why you can not pass Property values as "ref", but only local variables and fields)...
In case of extension methods, you are unable to get a reference to this so it is not possible to write extension methods for structs or enums which work the same way as their member methods.

I think a clean way would be, with using an extension method, is to explicitly return a new value and assign it to your variable/field/property, like this:


public enum Direction
{
	N, E, S, W,
}

public static class DirectionFunction
{
	public static Direction TurnLeft(this Direction direction)
	{
		switch (direction)
		{
			case Direction.N:
				return Direction.W;
			case Direction.W:
				return Direction.S;
			case Direction.S:
				return Direction.E;
			case Direction.E:
				return Direction.N;
			default:
				throw new ArgumentException("Invalid Direction value: " + direction);
		}
	}
}

public static class Program
{
	static void Main()
	{
		var direction = Direction.N;
		direction = direction.TurnLeft();
	}
}

Br. ^_^

Blog | Overburdened | KREEP | Memorynth | @blindmessiah777 Magic Item Tech+30% Enhanced GameDev

Thanks for the reply. It helped alot.

This topic is closed to new replies.

Advertisement