473,725 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(t he enumeration class or instance or whatever works)
{
for each (value in the given enumeration)
Console::WriteL ine(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 1053
"Mordachai" <Mo*******@news groups.nospamwr ote in message
news:%2******** **********@TK2M SFTNGP05.phx.gb l...
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(t he enumeration class or instance or whatever works)
{
for each (value in the given enumeration)
Console::WriteL ine(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::Reflect ion;

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

void PrintValuesOf(T ype^ enumType)
{
if (!enumType->IsEnum)
return;
for each (MemberInfo^ mi in
enumType->GetMembers(Bin dingFlags::Stat ic|BindingFlags ::Public))
{
if (mi->MemberType != MemberTypes::Fi eld)
continue;
Console::WriteL ine("Name: {0} Value:
{1}",mi->Name,(int)Enum ::Parse(enumTyp e,mi->Name));
}
}

void main()
{
PrintValuesOf(C olors::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
1342
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 describes a solution but I'm not able to port the if to C# : VB --- Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object,
1
2146
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 PropertyGrids displaying these enumerations (one of which has a few hundred items in). I believe this is because the dynamically generated enumerations are not being destroyed. I have not been able to find a mechanism for deleting/destroying dynamically...
1
2625
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 experimenting a bit with what I need to do this and have the following code snippet. But first if I pass the assembly name and type to Activator.CreateInstance() it always fails. However if I walk my assembly and get a type value, the call to...
0
1469
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 doing: I have an attribute defined as follows: public class CodeAttribute : Attribute
4
3535
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. Should a single class be created that contains all of a solution's enumerations? Or should enumerations be defined directly in the files that contain the related classes? Are there any other design patterns I am overlooking?
4
997
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
2311
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
2670
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 provide a "one obvious way" to do enumerations in Python. > >>> from enum import Enum > >>> day = Enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')
77
3937
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 reStructuredText source as it is today. Please discuss it here so I can see what issues people may have.
0
9257
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
9179
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
9116
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
6011
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
4519
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...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
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.