HOWTO? Iterate all legal values of an ENUM

L

Lee Gillie

I see this was posted a long time ago, but it has since expired.

Given....

Public Enum Fruit
Apple
Banana
Cherry
Peach
Strawberry
Tomatoe
End Enum

I'd like to be do something of this flavor in VB.NET....

Dim frMyFruit as Fruit

For Each frMyFruit in Fruit
...
Next

Of course it complains that "Fruit" is a type in the FOR EACH.
Does the language or runtime provide SOME WAY to find all legal values?

TIA for your learned advice.
Best regards,
Lee Gillie, Spokane
 
L

Lee Gillie

Never mind, I think I see it...

Dim frMyFruit as Fruit

For Each frMyFruit In [Enum].GetValues(GetType(Fruit))
...
Next

- Lee
 
H

Herfried K. Wagner [MVP]

Lee Gillie said:
Given....

Public Enum Fruit
Apple
Banana
Cherry
Peach
Strawberry
Tomatoe
End Enum

I'd like to be do something of this flavor in VB.NET....

Dim frMyFruit as Fruit

For Each frMyFruit in Fruit
...
Next

Of course it complains that "Fruit" is a type in the FOR EACH.
Does the language or runtime provide SOME WAY to find all legal values?

\\\
For Each f As Fruit In [Enum].GetValues(GetType(Fruit))
...
Next f
///
 
Top