473,799 Members | 3,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(t ypeof(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 36304
Try the Enum.GetNames() method.

"cyshao" <ad****@263.net > wrote in message
news:e6******** ******@TK2MSFTN GP09.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(t ypeof(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.WriteLi ne(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 FriendlyNameAtt ribute : Attribute
{
public readonly string Value;
public FriendlyNameAtt ribute(string value)
{
Value = value;
}
}

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

class MainClass
{
public static void Main(String[] args)
{
foreach (FieldInfo fi in typeof(MyEnum). GetFields())
{
FriendlyNameAtt ribute[] names =
(FriendlyNameAt tribute[])fi.GetCustomAt tributes(typeof (FriendlyNameAt tribute),
true);
if (names.Length > 0)
{
Console.WriteLi ne(names[0].Value);
}
else
{
Console.WriteLi ne(fi.Name);
}
}
}
}
HTH,
Alexander
"cyshao" <ad****@263.net > wrote in message
news:e6******** ******@TK2MSFTN GP09.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(t ypeof(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*******@omni talented.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.WriteLi ne(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.co m>
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
3141
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 conversion is undefined unless the value is within the range of the enumeration. For example: enum flag { x=1, y=2, z=4, e=8 }; // range 0:15 flag f1 = 5; // type error: 5 is not of type flag flag f2 = flag(5); // ok: flag(5) is of type flag and...
3
15108
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 Error End Enum
12
5420
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
7322
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 combobox, but I can't find anything that will let me do that. - Don
9
53806
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
1994
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 do this? Like a foreach loop. Thanks for any help, CK
8
1936
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 the array m_columnBlowStep is string because I have used ToString on each enum item as you can see. I want to use enum in the case statment in the switch. I know I can use string but I rather want to use enum.
3
5235
by: shapper | last post by:
Hello, I have an enum: Public Enum Color Red Blue Green End Enum
1
2665
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 on only the selected records. I have tried a couple of different methods without success. This is my current code:
1
6025
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
9538
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
10470
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10214
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
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...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6803
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
5459
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...
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2935
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.