Jump to content
  • Advertisement
Sign in to follow this  
QNAN

C# Enum extension function - impossible to set value?

This topic is 926 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
		}
	}
}
Edited by QNAN

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!