Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an array of enums

Tags:

arrays

c#

enums

I have about 30 different flagged enums that I would like to put into an array for indexing and quick access. Let me also claify that I do not have 1 enum with 30 values but I have 30 enums with differing amounts of values.

The goal would be to add them to an array at a specifed index. This way I can write a functions in which I can pass the array index into for setting particuler values of the enum.

Updated: Here is an example of what I am wanting to do.

enum main( enum1 = 0, enum2 = 1, enumn = n-1 ) - this has indexs which would match the index of the associated enum

[flag] enum1(value1=0, value2=1, value3=2, value4=4...)

[flag] enum2("")

[flag] enum2("")

since I am using flagable enums I have a class like the following

public static class CEnumWorker
{
   public static enum1 myEnum1 = enum1.value1;
   public static enum2 myEnum2 = enum2.value1;
   public static enumN myEnumN = enumN.value1;

   //I would then have functions that set the flags on the enums. I would like to access the enums through an array or other method so that I do not have to build a large switch statement to know which enum I am wanting to manipulate
}
like image 270
user381347 Avatar asked Jul 01 '10 17:07

user381347


1 Answers

Since you have 30 different types of enums, you can't create a strongly typed array for them. The best you could do would be an array of System.Enum:

Enum[] enums = new Enum[] { enum1.Value1, enum2.Value2, etc };

You would then have to cast when pulling an enum out of the array if you need the strongly typed enum value.

like image 142
Wesley Wiser Avatar answered Oct 05 '22 08:10

Wesley Wiser