473,406 Members | 2,343 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,406 software developers and data experts.

How to loop each items in enum ?

Hi ,my friends:

I have a enum type ,EMyEnum , defigned as [Flag()].
I just want to do something like this:

foreach( EMyEnum crrEnum in EMyEnum )
{
//do something
}

////////////////////////////////////////////////////
But I can't find any way to do that, and only used a very foolish method
like this

string[] names = Enum.GetNames(typeof(EMyEnum ));
foreach (string crrEnumName in names )
{
EMyEnum crrEnum = (EMyEnum )Enum.Parse( typeof(EMyEnum ) ,
crrEnumName );
//do something
}

///////////////////////////////////////////////////
Are there any good ideas to do that?
Or Microsoft forget to design a method like this:
vector< EMyEnum > Enum.GetTypes( typeof(EMyEnum ) )

Thanks

CYShao ^_^



Nov 16 '05 #1
3 36165
Try the Enum.GetNames() method.

"cyshao" <ad****@263.net> wrote in message
news:e6**************@TK2MSFTNGP09.phx.gbl...
Hi ,my friends:

I have a enum type ,EMyEnum , defigned as [Flag()].
I just want to do something like this:

foreach( EMyEnum crrEnum in EMyEnum )
{
//do something
}

////////////////////////////////////////////////////
But I can't find any way to do that, and only used a very foolish method
like this

string[] names = Enum.GetNames(typeof(EMyEnum ));
foreach (string crrEnumName in names )
{
EMyEnum crrEnum = (EMyEnum )Enum.Parse( typeof(EMyEnum ) ,
crrEnumName );
//do something
}

///////////////////////////////////////////////////
Are there any good ideas to do that?
Or Microsoft forget to design a method like this:
vector< EMyEnum > Enum.GetTypes( typeof(EMyEnum ) )

Thanks

CYShao ^_^


Nov 16 '05 #2
Well, if your method is foolish then I've been posing as a fool hundreds of
times! :-)

I routinely use Enum.GetNames to fill a combobox for instance and then
Enum.GetValues to get the selected value. Did you look at GetValues method?
Your sample could be rewritten as:

foreach (EMyEnum val in Enum.GetValues(typeof(EMyEnum)))
{
Console.WriteLine(val);
}
And then there's Reflection of course. Although an overkill in most
enum-related tasks, it allows some neat tricks like working with custom
attributes:

class FriendlyNameAttribute : Attribute
{
public readonly string Value;
public FriendlyNameAttribute(string value)
{
Value = value;
}
}

enum MyEnum {
[FriendlyName("Value of One")]
One,
[FriendlyName("Value of Two")]
Two,
Three
};

class MainClass
{
public static void Main(String[] args)
{
foreach (FieldInfo fi in typeof(MyEnum).GetFields())
{
FriendlyNameAttribute[] names =
(FriendlyNameAttribute[])fi.GetCustomAttributes(typeof(FriendlyNameAttribu te),
true);
if (names.Length > 0)
{
Console.WriteLine(names[0].Value);
}
else
{
Console.WriteLine(fi.Name);
}
}
}
}
HTH,
Alexander
"cyshao" <ad****@263.net> wrote in message
news:e6**************@TK2MSFTNGP09.phx.gbl...
Hi ,my friends:

I have a enum type ,EMyEnum , defigned as [Flag()].
I just want to do something like this:

foreach( EMyEnum crrEnum in EMyEnum )
{
//do something
}

////////////////////////////////////////////////////
But I can't find any way to do that, and only used a very foolish method
like this

string[] names = Enum.GetNames(typeof(EMyEnum ));
foreach (string crrEnumName in names )
{
EMyEnum crrEnum = (EMyEnum )Enum.Parse( typeof(EMyEnum ) ,
crrEnumName );
//do something
}

///////////////////////////////////////////////////
Are there any good ideas to do that?
Or Microsoft forget to design a method like this:
vector< EMyEnum > Enum.GetTypes( typeof(EMyEnum ) )

Thanks

CYShao ^_^


Nov 16 '05 #3
Alexander Shirshov <al*******@omnitalented.com> wrote:
Well, if your method is foolish then I've been posing as a fool hundreds of
times! :-)
I don't think so...
I routinely use Enum.GetNames to fill a combobox for instance and then
Enum.GetValues to get the selected value. Did you look at GetValues method?

Your sample could be rewritten as:

foreach (EMyEnum val in Enum.GetValues(typeof(EMyEnum)))
{
Console.WriteLine(val);
}


And that's the difference - I'd expect that calling GetValues is much
cheaper than calling GetNames and then calling Enum.Parse on each name.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

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

Similar topics

9
by: AngleWyrm | last post by:
"The C++ Programming Language" by Bjarne Stroustrup, copyright 1997 by AT&T, section 4.8 (pp 77): "A value of integral type may be explicitly converted to an enumeration type. The result of such a...
3
by: giant food | last post by:
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...
12
by: Mike Smith | last post by:
Hey anyone knows how to find an item in a list view based on text ? Cant seem to get the IndexOf method working. would the LVM_FINDITEM method using SendMessage API work in .Net ?
9
by: Don | last post by:
Is there any way to detect when an item has been added to the Items collection of a combobox or listbox? I am inheriting a Combobox and want to validate items before they are added to the...
9
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) { ... }
8
by: CK | last post by:
Hi All, Good morning. I had a quick question. I have a public Enum. During page load I want to loop thru the Enum and put those as items in an asp:dropdownList. Does anyone have any sample code to...
8
by: tony | last post by:
Hello! I have below a for loop and a switch in the for loop. I have also a enum called colBlowStep with some values. I have also an array called m_columnBlowStep with some strings. All items in...
3
by: shapper | last post by:
Hello, I have an enum: Public Enum Color Red Blue Green End Enum
1
by: Dale | last post by:
Access 2003 Front Connected to SQL 2005 Backend I have a multi-select listbox that shows a filtered list according to a date range referenced from a ctrl on the form. I need to update 2 fields...
1
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...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.