473,839 Members | 1,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne(item.ToStrin g)
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 15120
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.WriteLi ne(item.ToStrin g)
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(ty peof(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(GetT ype(statusValue s))
Console.WriteLi ne(item.ToStrin g)
Next
If you want the names of the Enum instead of the values, try:
For Each item in [Enum].GetNames(GetTy pe(statusValues ))
Console.WriteLi ne(item.ToStrin g)
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**********@y ahoo.com> wrote in message
news:a6******** *************** ***@posting.goo gle.com... 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.WriteLi ne(item.ToStrin g)
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**********@ya hoo.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.WriteLi ne(item.ToStrin g)
Next


\\\
Dim s As String
For Each s In [Enum].GetNames(GetTy pe(KnownColor))
Me.ListBox1.Ite ms.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
4863
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
0
1752
by: Vaclav Haisman | last post by:
Motivation: I have been working on some project recently that uses lots of enums with disjunctive intervals of values because it is rather convenient way to define series of constants with consecutive values. The problem is that some functions should only accept some of the ranges. To store values of more than one range/enum in variable one has to use int as storage which imho compromises type safety because any function that wants to...
3
16081
by: Richard | last post by:
Okay gang, This should be simple but apparently it's not... I want to use the System.DayOfWeek enum to create and access an array of objects with one object for each day of the week. I'd like the array declaration to be strongly typed to the enum and I'd like to use the enum to access entries in the array. I'd like to write code something like this: #using System;
13
3073
by: Adam Blair | last post by:
Is it possible to bind a switch statement to an Enum such that a compile-time error is raised if not all values within the Enum are handled in the switch statement? I realise you can use default: to catch unhandled cases, but of course this is only at run-time. Example: public enum MyEnum { one, two, three, four }
18
11389
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
9
53820
by: Fred Zwarts | last post by:
What is the recommended way to loop over all enum values of a certain enum type? Consider the following definition: typdef enum {A=2, B, C=5, D} E; then for (E x = A; x <= D; ++x) { ... }
34
11216
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
6
10065
by: bsma1 | last post by:
I building a web service that has an enum I want the consuming application to be able to use. I have the enum declared in the web service as: public enum myEnum { ONE = 1, TWO = 2, };
1
6028
by: jerry | last post by:
i have written a simple phonebook program,i'll show you some of the codes,the program's head file is member.h . i suppose the head file works well.so i don't post it. here's the clips of main function which i think has problem // this a simple program about phonebook,it can add items,del items,find items and #include "member.h" #include <fstream> #include <vector>
0
9856
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9698
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10597
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10299
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5684
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.