Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
swdev:dotnet:enum [2011/03/09 10:36]
smayr created
swdev:dotnet:enum [2011/12/02 14:11] (current)
smayr
Line 13: Line 13:
 To convert an enum value to string: To convert an enum value to string:
 <code csharp> <code csharp>
-public string GetEnumString(TMyColorobj)+public string GetEnumString(TMyColor obj)
 { {
      return Enum.GetName(typeof(TMyColor), obj);      return Enum.GetName(typeof(TMyColor), obj);
Line 21: Line 21:
 To convert a string into an enum: To convert a string into an enum:
 <code csharp> <code csharp>
-string strcolor = Enum.Parse(typeof(string), "Red");+TMyColor mycolor (TMyColor)Enum.Parse(typeof(TMyColor), "Red"); 
 +</code> 
 + 
 +== Enum with Description == 
 + 
 +<code csharp> 
 +public enum TMyColor 
 +
 +  [Description("Red Color")] 
 +  Red, 
 +  [Description("Green Color")] 
 +  Green, 
 +  [Description("Blue Color")] 
 +  Blue 
 +
 +</code> 
 + 
 +To simply get the enum string equivalent (translate value into string): 
 +<code csharp> 
 +//---------------------------------------------------------------------------------------- 
 +// Get enum string for the specified object. 
 +//---------------------------------------------------------------------------------------- 
 +public static string GetEnumString(TMyColorobj) 
 +
 +  return Enum.GetName(typeof(TMyColor), obj); 
 +
 +</code> 
 + 
 +To query an enum value for its description: 
 +<code csharp> 
 +using System.ComponentModel; 
 +... 
 + 
 +//---------------------------------------------------------------------------------------- 
 +// Get enum description string for the specified object. 
 +//---------------------------------------------------------------------------------------- 
 +public static string GetEnumDescription(Enum value) 
 +
 +    System.Reflection.FieldInfo fi = value.GetType().GetField(value.ToString()); 
 + 
 +    DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); 
 + 
 +    if ((attributes != null) && (attributes.Length > 0)) 
 +    { 
 +        return attributes[0].Description; 
 +    } 
 +    else 
 +    { 
 +        return value.ToString(); 
 +    } 
 +
 +</code> 
 + 
 +== Enumerate Enum for creating ListBox == 
 +<code csharp> 
 +///---------------------------------------------------------------------------------------- 
 +/// <summary> 
 +/// Enumerate enums to easily create a drop down list based on an enum. 
 +/// Source: http://blog.spontaneouspublicity.com/post/2008/01/17/Associating-Strings-with-enums-in-C.aspx 
 +///  
 +/// Example: 
 +/// DropDownList stateDropDown = new DropDownList(); 
 +/// foreach (States state in EnumToList<States>()) 
 +/// { 
 +///    stateDropDown.Items.Add(GetEnumDescription(state)); 
 +/// } 
 +/// </summary> 
 +/// <typeparam name="T"></typeparam> 
 +/// <returns></returns> 
 +///---------------------------------------------------------------------------------------- 
 +public static IEnumerable<T> EnumToList<T>() 
 +
 +    Type enumType = typeof(T); 
 + 
 +    // Can't use generic type constraints on value types, 
 +    // so have to do check like this 
 +    if (enumType.BaseType != typeof(Enum)) 
 +    { 
 +        throw new ArgumentException("T must be of type System.Enum"); 
 +    } 
 + 
 +    Array enumValArray  = Enum.GetValues(enumType); 
 +    List<T> enumValList = new List<T>(enumValArray.Length); 
 + 
 +    foreach (int val in enumValArray) 
 +    { 
 +        enumValList.Add((T)Enum.Parse(enumType, val.ToString())); 
 +    } 
 + 
 +    return enumValList; 
 +}
 </code> </code>