如何创建枚举数组

发布于 2024-09-08 01:39:27 字数 817 浏览 2 评论 0原文

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

目标是将它们添加到数组的指定索引处。这样我就可以编写一个函数,在其中可以将数组索引传递到其中以设置枚举的微粒值。

更新: 这是我想做的一个例子。

枚举主( 枚举1 = 0, 枚举2 = 1, 的索引相匹配的索引

enumn = n-1 ) - 这具有与关联枚举[flag] enum1(值1=0, 值2=1, 值3=2, 值4=4...)

[标志] enum2("")

[标志] enum2("")

因为我使用的是可标记枚举,所以我有一个如下所示的类

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
}

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月光色 2024-09-15 01:39:27

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

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

如果需要强类型枚举值,则在从数组中提取枚举时必须进行强制转换。

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.

澜川若宁 2024-09-15 01:39:27

如果我理解正确,你必须这样做:

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

但是你必须像这样访问(不是强类型,并且很容易引用错误的索引):

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

在.NET 4中,你可以使用元组:

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

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

...但是我不会推荐它用于这么多(30)个枚举,我认为甚至有8个左右的限制。您还可以创建自己的类:

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)
...

我建议使用最后一种方法,因为您使用的是引用类型,不依赖于索引位置,您可以为属性提供任何您想要的名称,并且它是强类型的。您唯一“失去”的是轻松迭代列表的能力,但如果您确实需要,可以使用反射来实现这一点。

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.

痴情换悲伤 2024-09-15 01:39:27

您始终可以使用旧的 object[],但这意味着需要进行大量转换。

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

や三分注定 2024-09-15 01:39:27

Enum 提供了两种将整数转换为枚举值的机制 - GetValues() 方法和普通转换:

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);
    }
}

如果您可以使用这些机制之一从国际。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文