Hi Plater,
you got me with this one !!
I remember doing something like this a few weeks ago, unfortunately had to scrap it.
this iterates the values
- foreach (PerhipType ptype in System.Enum.GetValues(typeof(PerhipType )))
-
{
-
//code is here
-
}
this iterates the names
- foreach (string str in System.Enum.GetNames(typeof(PerhipType )))
-
{
-
//code is here
-
}
I think it is like that !!!
This is how I have it in one of my projects...with names changed to protect those otherwise incriminated ;)
VB for you because I know how you hate that :oP
- Dim MyEnumItems() As MyEnum = System.Enum.GetValues(GetType(MyEnum))
-
For Each EnumItem As MyEnum In MyEnumItems
-
Console.WriteLine(EnumItem & ": " & CType(EnumItem, Integer))
-
Next
C#
- MyEnum[] MyEnumItems = (MyEnum[])System.Enum.GetValues(typeof(MyEnum));
-
foreach (MyEnum EnumItem in MyEnumItems){
-
Console.WriteLine(EnumItem+": "+(int)EnumItem);
-
}
...and GetNames
just gets the string names and GetValues actually gets the enumeration item as opposed to
just the value which means you have access to both the value
and the string.
If you use GetValues and you only require the value, cast it as an integer, otherwise by reference to the EnumItem you get the string name.