C# Typecast an Enum to a Struct
I'm using C#, and have manipulate an object one the screen. I setup a
struct to hold the coordinates, and then I setup an enum to restrict the
number of directions that could be moved.
private enum Direction
{
UpperLeft = new Coord(-1, -1), UpperCenter = new Coord(0, -1),
UpperRight = new Coord(1, -1),
Left = new Coord(-1, 0), Right = new Coord(1, 0),
LowerLeft = new Coord(-1, 1), LowerCenter = new Coord(0, 1),
LowerRight = new Coord(1, 1)
};
private struct Coord
{
public int row { get; private set; }
public int col { get; private set; }
public Coord(int r, int c) : this()
{
row = r;
col = c;
}
public static Coord operator +(Coord a, Coord b)
{
return new Coord(a.row + b.row, a.col + b.col);
}
}
Basically what I am aiming to do is to have the object I have on the
screen move based on the assigned enum.
So I'd like to hypothetically use it like this or something:
public class ThingThatMovesToTheLeft
{
Direction dir = Direction.Left;
Coord pos = new Coord(0,0);
public void Move()
{
this.pos = this.dir + this.pos;
}
}
Esentially my question is how do I typecast my enum back to my struct so I
can use it in this manor? I can't seem to be able to cast it back to the
struct. (Additionally, VisualStudios let me assign the enum to those Coord
structs without complaining so I assumed that it was okay to assign an
struct to an enum, is this acceptable practice or should this just not be
done?)
No comments:
Post a Comment