473,385 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Iterate through Enum values

Assume you've declared an enum ....

public enum MyEnum { First, Second, Third }

Without knowing the values of the enum, is it possible to iterate through
all the possible values programmatically?

Thanks in advance.

Mark
Nov 16 '05 #1
2 61027
Mark wrote:
Assume you've declared an enum ....

public enum MyEnum { First, Second, Third }

Without knowing the values of the enum, is it possible to iterate through
all the possible values programmatically?


Use the static System.Enum.GetNames method.

enum Color:
Red
Green
Blue

for item in System.Enum.GetNames(typeof(Color)):
print item

Nov 16 '05 #2
Mark,
You can use Enum.GetValues to get the list of values that belong to an Enum.
You can use Enum.GetNames to get the list of names of the values that belong
to an Enum.

Something like (untested):

foreach(MyEnum value in Enum.GetValues(typeof(MyEnum)))
{
// do something interesting with the value...
}

Hope this helps
Jay

"Mark" <fi******@idonotlikejunkmail.umn.edu> wrote in message
news:es***************@TK2MSFTNGP14.phx.gbl...
Assume you've declared an enum ....

public enum MyEnum { First, Second, Third }

Without knowing the values of the enum, is it possible to iterate through
all the possible values programmatically?

Thanks in advance.

Mark

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
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...
3
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...
3
by: Billy Porter | last post by:
Greetings, If I populate an ArrayList with values from, let's say, the Keys enum: ArrayList a = new ArrayList(); a.Add(Keys.Home); a.Add(Keys.End); ....then how do I iterate through the...
18
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
2
by: John A Grandy | last post by:
how to iterate through the members of an Enum ? Enum EnumAction None = 0 Action1 = 1 Action2 = 2 Action3 = 3 Action4 = 4 End Enum
2
by: Lee Gillie | last post by:
I see this was posted a long time ago, but it has since expired. Given.... Public Enum Fruit Apple Banana Cherry Peach Strawberry
6
by: Steven Woody | last post by:
is there any way in C leting me iterate all integer constants in a enum type ? their values might not be continue. thanks. - woody
34
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...
2
by: garth | last post by:
Hi, Is it possible to iterate over and enumerated type in C#? If so please can you paste a simple exampel up that covers the whole process. Thanks
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.