是否有可能投整数枚举? [英] Is it possible to cast integer to enum?

查看:110
本文介绍了是否有可能投整数枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
  演员的int枚举在C#

我得到了以下枚举:

 公开枚举detallistaDocumentStatus {

    ///<备注/>
    原版的,

    ///<备注/>
    复制,

    ///<备注/>
    REEMPLAZA,

    ///<备注/>
    删除,
}
 

后来我型detallistaDocumentStatus的类属性:

 公共detallistaDocumentStatus documentStatus {
        得到 {
            返回this.documentStatusField;
        }
        组 {
            this.documentStatusField =价值;
        }
    }
 

在现实生活中,用户将发送给我们,他们在声明的顺序一个数字(1,2,3或4)再presenting每个枚举值。

那么,有没有可能投这样?

  det.documentStatus =(detallistaDocumentStatus)3;
 

如果没有,我怎么能得到使用整数作为索引,我们使用了很多枚举的枚举值,所以我们想要做的事一般的和可重复使用的<​​/ P>

解决方案

是的,这是可能的投枚举 INT ,反之亦然,因为每个枚举实际上是重新按默认psented由 INT $ P $。您应当手动指定成员的值。 默认情况下,开始从0到N。

它也可以施放枚举字符串,反之亦然。

 公开枚举MyEnum
{
    值1 = 1,
    值2 = 2,
    值3 = 3
}

私有静态无效的主要(字串[] args)
{
    INT enumAsInt =(INT)MyEnum.Value2; // enumAsInt == 2

    INT myValueToCast = 3;
    字符串myValueAsString =值1;
    MyEnum myValueAsEnum =(MyEnum)myValueToCast; //将以值3

    MyEnum myValueAsEnumFromString;
    如果(Enum.TryParse&LT; MyEnum&GT;(myValueAsString,出myValueAsEnumFromString))
    {
        //把这里的逻辑
        // myValueAsEnumFromString将值1
    }

    到Console.ReadLine();
}
 

Possible Duplicate:
Cast int to Enum in C#

I got the following enum:

 public enum detallistaDocumentStatus {

    /// <remarks/>
    ORIGINAL,

    /// <remarks/>
    COPY,

    /// <remarks/>
    REEMPLAZA,

    /// <remarks/>
    DELETE,
}

then I got a class property of type detallistaDocumentStatus:

 public detallistaDocumentStatus documentStatus {
        get {
            return this.documentStatusField;
        }
        set {
            this.documentStatusField = value;
        }
    }

In the real life the user will send us a number (1, 2, 3 or 4) representing each enum value in the order they are declared.

so, is it possible to cast like this?

det.documentStatus = (detallistaDocumentStatus)3;

if not, how could I get the enum value using an integer as an index, we are using a lot of enums, so we want to do something generic and reusable

解决方案

Yes, it's possible to cast Enum to int and vice versa, because every Enum is actually represented by an int per default. You should manually specify member values. By default it starts from 0 to N.

It's also possible to cast Enum to string and vice versa.

public enum MyEnum
{
    Value1 = 1,
    Value2 = 2,
    Value3 = 3
}

private static void Main(string[] args)
{
    int enumAsInt = (int)MyEnum.Value2; //enumAsInt == 2

    int myValueToCast = 3;
    string myValueAsString = "Value1";
    MyEnum myValueAsEnum = (MyEnum)myValueToCast;   // Will be Value3

    MyEnum myValueAsEnumFromString;
    if (Enum.TryParse<MyEnum>(myValueAsString, out myValueAsEnumFromString))
    {
        // Put logic here
        // myValueAsEnumFromString will be Value1
    }

    Console.ReadLine();
}

这篇关于是否有可能投整数枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆