473,748 Members | 7,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# enum members at runtime

I have a need to convert the names of the members of an enumeration into an
array of strings at runtime.

I have determined a method using reflection.

Type theType = typeof(MyEnumNa me);
MemberInfo[] theMembers = theType.getMemb ers();

foreach (MemberInfo entry in theMembers)
{
if ((entry.MemberT ype.ToString() == "Field") && (entry.Name !=
"value__))
{
// add this entry.Name to the list
}
}

Unfortunately this method means that I do not know how many members there
are in the enumeration until I have scanned the entire MemberInfo array.

Is there a simpler or more direct method for achieving this?

-ken
Nov 16 '05 #1
5 15890
Ken Allen <no************ @sympatico.ca> wrote:
I have a need to convert the names of the members of an enumeration
into an array of strings at runtime.

I have determined a method using reflection.

Type theType = typeof(MyEnumNa me);
MemberInfo[] theMembers = theType.getMemb ers();

foreach (MemberInfo entry in theMembers)
{
if ((entry.MemberT ype.ToString() == "Field") && (entry.Name !=
"value__))
{
// add this entry.Name to the list
}
}

Unfortunately this method means that I do not know how many members
there are in the enumeration until I have scanned the entire
MemberInfo array.

Is there a simpler or more direct method for achieving this?


Yup -

string[] names = Enum.GetNames(t ypeof(MyEnumNam e));

--
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 #2
Cool.

Now, since this is a 'falg' (or bit field) enum, how can I get the value for
each of the names at the same time?

-ken

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Ken Allen <no************ @sympatico.ca> wrote:
I have a need to convert the names of the members of an enumeration
into an array of strings at runtime.

I have determined a method using reflection.

Type theType = typeof(MyEnumNa me);
MemberInfo[] theMembers = theType.getMemb ers();

foreach (MemberInfo entry in theMembers)
{
if ((entry.MemberT ype.ToString() == "Field") && (entry.Name != "value__))
{
// add this entry.Name to the list
}
}

Unfortunately this method means that I do not know how many members
there are in the enumeration until I have scanned the entire
MemberInfo array.

Is there a simpler or more direct method for achieving this?


Yup -

string[] names = Enum.GetNames(t ypeof(MyEnumNam e));

--
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 #3
Ken Allen <no************ @sympatico.ca> wrote:
Now, since this is a 'falg' (or bit field) enum, how can I get the value for
each of the names at the same time?


Well, there's Enum.GetValues as well, but I don't think there's any
guarantee that the order will be the same...

--
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
I was useing reflection to acheive the same results. I wanted to make the
values of an enumeration dynamically available to a user in a list box. In
order to do it I performed the following steps:

Type tEnum = typeof(DRW.Draw ing2D.Interpola tionMode);
Array aVals = Enum.GetValues( tEnum);

foreach(DRW.Dra wing2D.Interpol ationMode im in aVals)
{
String strName = im.ToString();
}

This worked for me.

"Jon Skeet [C# MVP]" wrote:
Ken Allen <no************ @sympatico.ca> wrote:
Now, since this is a 'falg' (or bit field) enum, how can I get the value for
each of the names at the same time?


Well, there's Enum.GetValues as well, but I don't think there's any
guarantee that the order will be the same...

--
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 #5
Hi Brian,

Instead of getting all valies and convert them to strings you can use
Enum.GetNames method

--

Stoitcho Goutsev (100) [C# MVP]
"Brian R." <8B************ *@noemail.nospa m> wrote in message
news:BD******** *************** ***********@mic rosoft.com...
I was useing reflection to acheive the same results. I wanted to make the
values of an enumeration dynamically available to a user in a list box.
In
order to do it I performed the following steps:

Type tEnum = typeof(DRW.Draw ing2D.Interpola tionMode);
Array aVals = Enum.GetValues( tEnum);

foreach(DRW.Dra wing2D.Interpol ationMode im in aVals)
{
String strName = im.ToString();
}

This worked for me.

"Jon Skeet [C# MVP]" wrote:
Ken Allen <no************ @sympatico.ca> wrote:
> Now, since this is a 'falg' (or bit field) enum, how can I get the
> value for
> each of the names at the same time?


Well, there's Enum.GetValues as well, but I don't think there's any
guarantee that the order will be the same...

--
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 #6

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

Similar topics

12
3424
by: Steven T. Hatton | last post by:
Any opinions or comments on the following? I don't say it below, but I came out on the side of using enumerations over static constants. /* I'm trying to figure out the pros and cons of using static const * int class members as opposed to using enumerations to accomplish a * similar goal. The use of static constants seems more explicit and * obvious to me. Unless I assign values to the enumerators, the * compiler will do it for me....
4
3053
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum { DOUBLE_LIST, INT_LIST } DATA_TYPE; typedef struct { DATA_TYPE type;
21
6364
by: Bilgehan.Balban | last post by:
Hi, I have two different enum definitions with members of same name. The compiler complains about duplicate definitions. Is this expected behaviour? Can't I have same-named fields in different enum definitions? I think it should have been perfectly valid to do that. Its a stupid limitation to have to define disjoint enum symbol definitions. This is like having to have disjoint member names in structures.
21
4600
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
2
1834
by: Michael.McD | last post by:
HI, I'd like to wire an enum's properties to a table in a dB at runtime. Has any one here tried this? Is it possible? And if it is how is it done? Thanks in advance, Michael McDowell
31
3616
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1(); Control c = f; An enum value inherits from int but it doesn't get implicitly converted: HorizontalAlignment h = HorizontalAlignment.Center;
13
12391
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
34
11197
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?
2
9214
by: Jay | last post by:
I have a few enums, a shortened example of one is: enum ParamTest {Err, Start, Block, Hold} Is there a programmatic way to find the number of "members" (probably the wrong C# term) of the Enum - this is 4 for the example given. One not very good way that I currently use is to add a dummy "LENGTH" member on the end: enum ParamTest {Err, Start, Block, Hold, LENGTH}
0
8984
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
9363
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...
1
9312
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
9238
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
8237
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...
1
6793
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
6073
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
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2206
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.