如何创建枚举数组

如何创建枚举数组

本文介绍了如何创建枚举数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将大约30个带标记的枚举放入数组中以进行索引和快速访问.我还要说明一下,我没有1个具有30个值的枚举,但我有30个具有不同数量的值的枚举.

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.

枚举main(enum1 = 0,枚举2 = 1,enumn = n-1)-具有与关联的枚举的索引匹配的索引

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

[标志]enum1(值1 = 0,值2 = 1,值3 = 2,值4 = 4 ...)

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

[标志]enum2(")

[flag]enum2("")

[标志]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
}

推荐答案

由于您有30种不同类型的枚举,因此无法为它们创建强类型的数组.您可能要做的最好是System.Enum的数组:

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.

这篇关于如何创建枚举数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:46