iterate Enum

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

how to iterate through the members of an Enum ?

Enum EnumAction
None = 0
Action1 = 1
Action2 = 2
Action3 = 3
Action4 = 4
End Enum

Dim vAction As EnumAction

For Each vAction In Enum1 ....

Next vAction

triggers error " 'EnumAction' is a type and cannot be used in an expression
"
 
you can do something like this:

Enum testenum
one = 1
two
three
four
five
End Enum

Dim arr As Array = [Enum].GetValues(GetType(testenum))
For i As Integer = arr.GetLowerBound(0) To arr.GetUpperBound(0)
Console.WriteLine("Name: {0}" & vbTab & "Value: {1}", _
[Enum].GetName(GetType(testenum), arr.GetValue(i)), _
CInt(arr.GetValue(i)))
Next

hope that helps..
Imran.
 
John,
In addition to Imran's sample, I normally use:

For Each vAction In [Enum].GetValues(GetType(EnumAction))
Debug.WriteLine(vAction)
Next vAction

In addition to System.Enum.GetValues which gets the values of an Enum, there
is Format, GetNames, IsDefined, Parse and a number of overloaded ToObject
shared methods each which are equally useful for the job they perform.

Hope this helps
Jay
 
Am I missing something? I don't see any instantiation so it seems that the
message stating that it is a type is correct and appropriate.
Serious quest, by the way.
BobJ
 
Back
Top