473,795 Members | 2,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looping through multiple enumerations

I'd like to create a list of enumerated types, and then loop through each of
those types to perform a standard operation on each. The following sample
gives an idea of what I'd like to accomplish, but it doesn't work and I don't
know the correct way to do this. Is this possible? If so, what should each
of my foreach loops look like? Thanks.

private enum Colors { Red, Yellow, Blue, Green };
private enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
private enum Months { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov,
Dec };

private Type[] enumTypes = { typeof(Colors), typeof(Days), typeof(Months) };

static void Main(string[] args)
{
// Go through each of the enumerated types
foreach (Type currentType in enumTypes)
{
Console.WriteLi ne( currentType.ToS tring() );
// Display the names of each of the members
foreach (string s in
((System.Enum)c urrentType).Get Names(typeof(cu rrentType)))
{
Console.WriteLi ne(s);
}
}
}

Oct 6 '08 #1
1 1116
Got the answer. My outer loop is actually correct. I was just referencing a
non-static array from within my static function.

The inner loop should be foreach(string s in
System.Enum.Get Names(currentTy pe))
"David R" wrote:
I'd like to create a list of enumerated types, and then loop through each of
those types to perform a standard operation on each. The following sample
gives an idea of what I'd like to accomplish, but it doesn't work and I don't
know the correct way to do this. Is this possible? If so, what should each
of my foreach loops look like? Thanks.

private enum Colors { Red, Yellow, Blue, Green };
private enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
private enum Months { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov,
Dec };

private Type[] enumTypes = { typeof(Colors), typeof(Days), typeof(Months) };

static void Main(string[] args)
{
// Go through each of the enumerated types
foreach (Type currentType in enumTypes)
{
Console.WriteLi ne( currentType.ToS tring() );
// Display the names of each of the members
foreach (string s in
((System.Enum)c urrentType).Get Names(typeof(cu rrentType)))
{
Console.WriteLi ne(s);
}
}
}
Oct 6 '08 #2

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

Similar topics

0
1180
by: Plinkerton | last post by:
I'm making an Base Class that will be inherited. In my base class, I have a public enumeration that defines a list of things I want my class to be able to do. I use it for Method input parameters, as well as output Events. Public Enum enmListOfValues Value1 Value2 Value3 Value4
1
2151
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
5613
by: Oleg Ogurok | last post by:
Hi all, I've added a new DataSet (xsd file) to my project in VS.NET 2003. There I create a simple type as an enumeration of values. <xs:simpleType name="MyCustomType"> <xs:restriction base="xs:string"> <xs:enumeration value="Apple" /> <xs:enumeration value="Orange" /> </xs:restriction>
4
3539
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?
1
1537
by: Hans De Schrijver | last post by:
I'm new to C# development, so here's a basic question. I'm developing a code library (no UI) where several of the classes will implement an IPersistable interface. Each class lives in its own class file (within the same project) but all classes are defined within the same namespace DaySpanLib. Now here's my question: given that the interface I need to develop will be implemented by several classes, each of which live in their own .cs...
6
5000
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
27
2678
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
3958
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.
13
5029
by: JayCallas | last post by:
I know this question has been asked. And the usual answer is don't use cursors or any other looping method. Instead, try to find a solution that uses set-based queries. But this brings up several questions / senarios: * I created several stored procedures that take parameters and inserts the data into the appropriate tables. This was done for easy access/use from client side apps (i.e. web-based).
0
1315
by: chitta | last post by:
Hi I have a problem when i am looping on multiple lines. the question as follows If OrderProductLineItem (DocType='OrderCreate'/'OrderChange') < OrderProductLineItem (DocType='OrderResponse') Loop on OrderProductLineItem (DocType='OrderResponse') PurchaseOrderLineItemNumber If PurchaseOrderLineItemNumbe has no data the copy from LineNumber (DocType='OrderResponse') ...
0
9522
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
10448
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...
0
10217
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
10167
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,...
1
7544
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
5440
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...
1
4114
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
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2922
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.