Connecting Tech Pros Worldwide Forums | Help | Site Map

loop over Enum values

giant food
Guest
 
Posts: n/a
#1: Nov 20 '05
This seems like a long shot, but I wondered if there's a way to loop
over the values in an Enum. For example, say I have an Enum as
follows:

Public Enum statusValues
Ready
Running
Finished
Error
End Enum

'.....

I'd like to be able to loop over and print out these values' names
something like this:

For Each item in statusValues
Console.WriteLine(item.ToString)
Next


....the reason being that, if I add elements to my Enum object, I won't
have to go change the code that prints out their names. Thanks!

--Frank



Roy Osherove
Guest
 
Posts: n/a
#2: Nov 20 '05

re: loop over Enum values


On 25 Nov 2003 07:30:10 -0800, giant food wrote:
[color=blue]
> This seems like a long shot, but I wondered if there's a way to loop
> over the values in an Enum. For example, say I have an Enum as
> follows:
>
> Public Enum statusValues
> Ready
> Running
> Finished
> Error
> End Enum
>
> '.....
>
> I'd like to be able to loop over and print out these values' names
> something like this:
>
> For Each item in statusValues
> Console.WriteLine(item.ToString)
> Next
>
> ...the reason being that, if I add elements to my Enum object, I won't
> have to go change the code that prints out their names. Thanks!
>
> --Frank[/color]

The Enum class has static methods that allow you to do this.
For example, to get the name of a specific enum value that was passed to
you, you could use
string name = Enum.GetName(typeof(MyEnumType),SomeEnumValue)


--
Roy Osherove
weblog: http://www.iserializable.com
Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#3: Nov 20 '05

re: loop over Enum values


Frank,
Remember, all enums inherit from System.Enum, System.Enum has shared methods
for retrieving information about Enums.

Try something like:[color=blue]
> For Each item in [Enum].GetValues(GetType(statusValues))
> Console.WriteLine(item.ToString)
> Next[/color]

If you want the names of the Enum instead of the values, try:
[color=blue]
> For Each item in [Enum].GetNames(GetType(statusValues))
> Console.WriteLine(item.ToString)
> Next[/color]

The [Enum] is an escaped identifier it allows me to refer to the System.Enum
type instead of the Enum keyword used to define an Enum.

Other useful members of System.Enum include: Format, GetName, IsDefined,
Parse, and ToObject.

Hope this helps
Jay

"giant food" <dinosaur8000@yahoo.com> wrote in message
news:a61ab53c.0311250730.3536c737@posting.google.c om...[color=blue]
> This seems like a long shot, but I wondered if there's a way to loop
> over the values in an Enum. For example, say I have an Enum as
> follows:
>
> Public Enum statusValues
> Ready
> Running
> Finished
> Error
> End Enum
>
> '.....
>
> I'd like to be able to loop over and print out these values' names
> something like this:
>
> For Each item in statusValues
> Console.WriteLine(item.ToString)
> Next
>
>
> ...the reason being that, if I add elements to my Enum object, I won't
> have to go change the code that prints out their names. Thanks!
>
> --Frank[/color]


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#4: Nov 20 '05

re: loop over Enum values


* dinosaur8000@yahoo.com (giant food) scripsit:[color=blue]
> This seems like a long shot, but I wondered if there's a way to loop
> over the values in an Enum. For example, say I have an Enum as
> follows:
>
> Public Enum statusValues
> Ready
> Running
> Finished
> Error
> End Enum
>
> '.....
>
> I'd like to be able to loop over and print out these values' names
> something like this:
>
> For Each item in statusValues
> Console.WriteLine(item.ToString)
> Next[/color]

\\\
Dim s As String
For Each s In [Enum].GetNames(GetType(KnownColor))
Me.ListBox1.Items.Add(s)
Next s
///

Replace 'KnownColor' with the name of your enum.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Closed Thread


Similar Visual Basic .NET bytes