472,102 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,102 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 35521
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by AngleWyrm | last post: by
3 posts views Thread by giant food | last post: by
12 posts views Thread by Mike Smith | last post: by
9 posts views Thread by Fred Zwarts | last post: by
8 posts views Thread by CK | last post: by
3 posts views Thread by shapper | last post: by
1 post views Thread by jerry | last post: by
reply views Thread by leo001 | last post: by

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.