This is an old revision of the document!


Enum

To declare an enumerated type:

public enum TMyColor
{
  Red,
  Green,
  Blue
}

To convert an enum value to string:

public string GetEnumString(TMyColor obj)
{
     return Enum.GetName(typeof(TMyColor), obj);
}

To convert a string into an enum:

TMyColor mycolor = (TMyColor)Enum.Parse(typeof(TMyColor), "Red");
Enum with Description
public enum TMyColor
{
  [Description("Red Color")]
  Red,
  [Description("Green Color")]
  Green,
  [Description("Blue Color")]
  Blue
}

To query an enum value for its description:

//----------------------------------------------------------------------------------------
// Get enum string for the specified object.
//----------------------------------------------------------------------------------------
public static string GetEnumString(TMyColorobj)
{
  return Enum.GetName(typeof(TMyColor), obj);
}