开发者

How to create an array of enums

I have about 30 different flagged enums that I would like to put into an array for开发者_Python百科 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
}


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.


If I understand correctly, you would have to do:

object[] enums = new object[30];
enums[0] = Enum1.Value1;
enums[1] = Enum2.AnotherValue;

But then you would have to access like this (not strongly typed, and easy to reference the wrong index):

if ((Enum1)enums[0] == Enum1.Value1)
...

In .NET 4, you could use the Tuple:

var enums = new Tuple<Enum1, Enum2>(Enum1.Value1, Enum2.AnotherValue);

if (enums.Item1 == Enum1.Value1)
...

...but I wouldn't recommend it for so many (30) enums, and I think there's even a limit of 8 or so. You can also create your own class:

class Enums
{
  public Enum1 Enum1 { get; set; }
  public Enum2 Enum2 { get; set; }
}

Enums enums = new Enums();
enums.Enum1 = Enum1.Value1;
enums.Enum2 = Enum2.AnotherValue;

if (enums.Enum1 == Enum1.Value1)
...

I would recommend the last way because you're using a reference type, you're not dependent on index position, you can give the properties whatever name you want, and it's strongly typed. The only thing you "lose" is the ability to easily iterate through the list, but you can achieve that with Reflection if you really need to.


You could always use good old object[], but that means doing a lot of casting.


Enum provides two mechanisms for converting an integer to an enum value - the GetValues() method and plain casting:

enum EnumA { A1, A2, A1234 }
enum EnumB { B00, B01, B02, B04 }

class Program
{
    static void Main(string[] args)
    {
        EnumA a = ((EnumA[])Enum.GetValues(typeof(EnumA)))[0];

        Console.WriteLine(a);

        EnumB boa = (EnumB)3;

        Console.WriteLine(boa);
    }
}

I'm not quite sure why you want to create your own arrays if you can use one of these mechanisms to get an enum from an int.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜