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

enumerations & reflection

I'm trying to simply generate the list of enumeration values for a given
enumeration class.

e.g., for the following:

enum struct Colors { red, green, blue };

I would like to have a function:

void PrintValuesOf(the enumeration class or instance or whatever works)
{
for each (value in the given enumeration)
Console::WriteLine(the name of the enumeration & its value);
}

Which would output:

red
green
blue

Thanks for any assistence!

--
Steven S. Wolf
Head of Software Development
Cimex Corporation
Aug 3 '06 #1
1 1043
"Mordachai" <Mo*******@newsgroups.nospamwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
I'm trying to simply generate the list of enumeration values for a given
enumeration class.

e.g., for the following:

enum struct Colors { red, green, blue };

I would like to have a function:

void PrintValuesOf(the enumeration class or instance or whatever works)
{
for each (value in the given enumeration)
Console::WriteLine(the name of the enumeration & its value);
}

Which would output:

red
green
blue

Thanks for any assistence!
Something like this -

<code>
using namespace System;
using namespace System::Reflection;

public enum struct Colors { red, green, blue };

void PrintValuesOf(Type^ enumType)
{
if (!enumType->IsEnum)
return;
for each (MemberInfo^ mi in
enumType->GetMembers(BindingFlags::Static|BindingFlags::Pub lic))
{
if (mi->MemberType != MemberTypes::Field)
continue;
Console::WriteLine("Name: {0} Value:
{1}",mi->Name,(int)Enum::Parse(enumType,mi->Name));
}
}

void main()
{
PrintValuesOf(Colors::typeid);
}

</code>

There might be a better way to get the value associated with a given member
name than using Enum.Parse, but if there is, I didn't find it.

-cd

Aug 4 '06 #2

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

Similar topics

1
by: phoenix | last post by:
Hello, I'm using the webbrowser control to display a webpage on my form. The first problem is the gets fired for every image which is completed as well. http://support.microsoft.com/?id=180366...
1
by: someone else | last post by:
I have some code that creates dynamic enumerations for use in a PropertyGrid control. This all works perfectly but the memory usage of the program increases quite quicly when viewing the...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
0
by: James Cohen | last post by:
Hi, I hope somebody can help me out, Im looking for a way to retreive the original filename & line number of a Reflection.MemberInfo instance. Ill provide some background on what it is that im...
4
by: ChrisB | last post by:
Hello: I will be creating 50+ enumerations related to a large number of classes that span a number of namespaces. I was wondering if there are any "best practices" when defining enumerations. ...
4
by: John Hom | last post by:
Greetings, I was wondering if there was a way of dynamically creating enumerations. For example, we could do it statically like: enum stuff zero, one, two,
38
by: One Handed Man \( OHM - Terry Burns \) | last post by:
I have a basic question thats been niggling me, but I never found time to look at it before. If I have an enumeration such as this Fiend Enum TravelDirection North South East
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
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
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
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...
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
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.