469,612 Members | 1,930 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

loop over Enum values

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
Nov 20 '05 #1
3 13981
On 25 Nov 2003 07:30:10 -0800, giant food wrote:
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


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
Nov 20 '05 #2
Frank,
Remember, all enums inherit from System.Enum, System.Enum has shared methods
for retrieving information about Enums.

Try something like:
For Each item in [Enum].GetValues(GetType(statusValues))
Console.WriteLine(item.ToString)
Next
If you want the names of the Enum instead of the values, try:
For Each item in [Enum].GetNames(GetType(statusValues))
Console.WriteLine(item.ToString)
Next
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" <di**********@yahoo.com> wrote in message
news:a6**************************@posting.google.c om... 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

Nov 20 '05 #3
* di**********@yahoo.com (giant food) scripsit:
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


\\\
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>
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Glenn Venzke | last post: by
reply views Thread by Vaclav Haisman | last post: by
3 posts views Thread by Richard | last post: by
13 posts views Thread by Adam Blair | last post: by
18 posts views Thread by Visual Systems AB \(Martin Arvidsson\) | last post: by
9 posts views Thread by Fred Zwarts | last post: by
34 posts views Thread by Steven Nagy | last post: by
6 posts views Thread by bsma1 | last post: by
1 post views Thread by jerry | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.