自己写了个枚举变量练习
enum test:int
{
i=1,
j=2,
k=4
}test = test.i;
text1.text = i.tostring();现在 text1.text显示的是i,而不是1, test = test.i+1显示的是j
test = test.i+2这时text1.text是显示的是3请问这是怎么回事?如果枚举变量中赋的值不能用的话 这个变量还有什么意义啊?

解决方案 »

  1.   

    你强制转换一下就可以了!(int)test.i
      

  2.   

    enum.GetName(Type enumType,Object value)
    这个函数好象可以检测
      

  3.   

    因为3在枚举项中没有对应成员。。所以就是显示数字3。。
    将枚举成员转换成默认值需要显式转换。。int a = (int)test.i;// 写成 int a = test.i; 就不行了。。
      

  4.   

    text1.text = (int)test.i;
    public enum Test
        {
            a,
            b,
            c
        }
        
     Test t= Test.a;
       //enum和int互相转换
     int i = (int)t; 
     t= (Test)(i+1);         
        //enum和string互相转换
     string s = t.ToString();
     t= (Test )Enum.Parse(t.GetType(),"a");