Reputation:
I've created a enum Direction
like so:
enum Direction { Up, Down, Left, Right }
Now these directions get called on arrow key presses. What I have now in my KeyDown
event is this:
if (e.KeyCode == Keys.Up)
{
moveDirection = Direction.Up;
}
else if (e.KeyCode == Keys.Left)
{
moveDireciton = Direction.Left;
}
else if ...
Is it possible to create my own cast like (Direction)Keys.Up
?
I know I could just create a static method static Direction CastKeysToDirection(Keys k);
and I'm fine with this, but I'm just curious if there's a way to implement our own casts in c#?
Upvotes: 2
Views: 972
Reputation: 25434
Implicit casting is not possible to enum, but you can use implicit operator in class implementation and then maybe use some benefits of polymorphism and encapsulation in further working (Included suggestion of @Chris Sinclair):
public abstract class Direction
{
public static implicit operator Direction(Keys key)
{
switch (key)
{
case Keys.Up:
return Up.Instance;
case Keys.Down:
return Down.Instance;
case Keys.Left:
return Left.Instance;
case Keys.Right:
return Right.Instance;
default:
throw new ArgumentOutOfRangeException();
}
}
}
public class Up : Direction
{
private static readonly Up _Instance = new Up();
public static Up Instance
{
get { return _Instance; }
}
private Up() {}
}
public class Down : Direction
{
private static readonly Down _Instance = new Down();
public static Down Instance
{
get { return _Instance; }
}
private Down() {}
}
public class Left : Direction
{
private static readonly Left _Instance = new Left();
public static Left Instance
{
get { return _Instance; }
}
private Left() {}
}
public class Right : Direction
{
private static readonly Right _Instance = new Right();
public static Right Instance
{
get { return _Instance; }
}
private Right() {}
}
The use case:
Keys key = Keys.Up;
Direction direction = key; // Legal
if (direction == Up.Instance)
{
// Do something
}
// Or add abstract methods to Direction class and invoke direction.DoWork()
Upvotes: 3
Reputation: 1500893
Two options:
Just cast, if you're sure the values match (e.g. because you've created the Direction
enum using Keys.Up
etc as the values):
moveDirection = (Direction) e.KeyCode;
Create a dictionary:
private static readonly Dictionary<Keys, Direction> KeyToDirection =
new Dictionary<Keys, Direction>
{
{ Keys.Up, Direction.Up },
{ Keys.Left, Direction.Left },
...
};
Then just look up the key that's been pressed in the dictionary. (Use TryGetValue
to look up a Keys
- use the return value to detect keys which aren't present in the dictionary.)
The latter gives a more flexible approach, of course. It allows you to have multiple keys all mapping to the same direction, for example.
Upvotes: 5
Reputation: 50114
If you define your enum as
enum Direction {
Up = Keys.Up,
Down = Keys.Down,
Left = Keys.Left,
Right = Keys.Right
}
then your cast (Direction)Keys.Up
should work by virtue of the fact that the numerical value underlying Keys.Up
is the same as the numerical value underlying Direction.Up
.
Sadly, though, you can't provide a true explicit
or impilcit
cast on an enum
like you can on a class
.
Upvotes: 7