Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
swdev:dotnet:enum [2011/12/02 14:09] smayr |
swdev:dotnet:enum [2011/12/02 14:11] (current) smayr |
||
---|---|---|---|
Line 74: | Line 74: | ||
</ | </ | ||
+ | == Enumerate Enum for creating ListBox == | ||
+ | <code csharp> | ||
+ | /// | ||
+ | /// < | ||
+ | /// Enumerate enums to easily create a drop down list based on an enum. | ||
+ | /// Source: http:// | ||
+ | /// | ||
+ | /// Example: | ||
+ | /// DropDownList stateDropDown = new DropDownList(); | ||
+ | /// foreach (States state in EnumToList< | ||
+ | /// { | ||
+ | /// stateDropDown.Items.Add(GetEnumDescription(state)); | ||
+ | /// } | ||
+ | /// </ | ||
+ | /// < | ||
+ | /// < | ||
+ | /// | ||
+ | public static IEnumerable< | ||
+ | { | ||
+ | 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(" | ||
+ | } | ||
+ | |||
+ | Array enumValArray | ||
+ | List< | ||
+ | |||
+ | foreach (int val in enumValArray) | ||
+ | { | ||
+ | enumValList.Add((T)Enum.Parse(enumType, | ||
+ | } | ||
+ | |||
+ | return enumValList; | ||
+ | } | ||
+ | </ |