473,397 Members | 1,949 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,397 software developers and data experts.

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(MyEnumName);
MemberInfo[] theMembers = theType.getMembers();

foreach (MemberInfo entry in theMembers)
{
if ((entry.MemberType.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 15871
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(MyEnumName);
MemberInfo[] theMembers = theType.getMembers();

foreach (MemberInfo entry in theMembers)
{
if ((entry.MemberType.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(typeof(MyEnumName));

--
Jon Skeet - <sk***@pobox.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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(MyEnumName);
MemberInfo[] theMembers = theType.getMembers();

foreach (MemberInfo entry in theMembers)
{
if ((entry.MemberType.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(typeof(MyEnumName));

--
Jon Skeet - <sk***@pobox.com>
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.com>
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.Drawing2D.InterpolationMode);
Array aVals = Enum.GetValues(tEnum);

foreach(DRW.Drawing2D.InterpolationMode 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.com>
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.nospam> wrote in message
news:BD**********************************@microsof t.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.Drawing2D.InterpolationMode);
Array aVals = Enum.GetValues(tEnum);

foreach(DRW.Drawing2D.InterpolationMode 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.com>
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
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...
4
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 {...
21
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...
21
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
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
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();...
13
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
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: 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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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
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...

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.