Looping through multiple enumerations

D

David R

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.WriteLine( currentType.ToString() );
// Display the names of each of the members
foreach (string s in
((System.Enum)currentType).GetNames(typeof(currentType)))
{
Console.WriteLine(s);
}
}
}
 
D

David R

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.GetNames(currentType))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top