Nahum
Nahum

Reputation: 7197

How to give two names to the same enum? (Type Name Alias)

I got a need for two exactly identical enums with different names

I can do

enum DoorStatus{Open,Closed,Locked,Broken};
enum WindowStatus{Open,Closed,Locked,Broken};

and will probably be bit easier to read.

but I'd rather not duplicate code

in C++ I'd do

typedef Door,Window;

what is the solution in C#?

and why have both you ask? I got Application That Handles a Window application that handles a Door.

I want the Window application developer to send me data using 'WindowStatus' enum the 'Door' guy use 'DoorStatus' enum.

I believe that user should know or care I got other devices that can be abstracted similarly.

EDIT: used

enum HatchStatus{Open,Closed,Locked,Broken};
using DoorStatus = HatchStatus;
using WndowStatus = HatchStatus;

EDIT2:

Error A using clause must precede all other elements defined in the namespace except extern alias declarations

:(

Upvotes: 9

Views: 12643

Answers (2)

Andras Zoltan
Andras Zoltan

Reputation: 42333

I suppose the question is if you have two different enums that have the exact same values - then why keep both at all?

The best solution is to pick one and refactor everything to use just one.

Update

Now that you've updated your example, I think you should consider whether an enum is actually the best solution.

public class ObjectState
{
  public int ID { get; set; }
  public string Description { get; set; }
}//should really be immutable
 //should also implement IEquatable<ObjectState> for equality

public static readonly State_Open = new ObjectState()
  { ID = 1, Description = "Open" }
public static readonly State_Closed = new ObjectState() 
  { ID = 2, Description = "Closed" }

public static class DoorStatus
{ 
   public static readonly ObjectState Open = State_Open;
   public static readonly ObjectState Closed = State_Closed; 
}
//and WindowStatus if you want.


public class Door {
  public ObjectState State { get; set; }
} //or perhaps refactor to a shared base/interface StatefulObject

Now you can simply do:

new Door() { State = DoorStatus.Open; }

If you also implement IEquatable<ObjectState> then you can compare the equality of two object's state as well; you'd then likely implement GetHashCode as well meaning you can also use a state as a key in a grouping or dictionary.

The good thing now is that both an open window and an open door now have exactly the same state that equate directly, rather than using some syntactic sugar to get around a design that required identical enums.

Final Update

Since the above is 'horribly complex' - despite being, I think, good design; the best you can do is ensure that two separate enums at least share the same underlying values for identical states:

public enum States
{
  Open
}

public enum DoorStates
{
  Open = States.Open
}

public enum WindowStates
{
  Open = States.Open
}

Ultimately if you're using an enum you cannot get around the need to have two separately defined ones - this solution simply means that Open is equal for any object that can be in an Open state.

Upvotes: 6

Thomas Levesque
Thomas Levesque

Reputation: 292375

If you just want an alias, you can use a using directive:

using Enum2 = Enum1;

Upvotes: 21

Related Questions