枚举问题 [英] problem with enumeration

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

问题描述

我写了公开enum cmd {login = 11,logout = 12};

i想要使用那些值,当我把字符串a = cmd.login它需要注销但我想要登录值,这是11帮助我吗?

解决方案

如果你真的想要与Enum'cmd 转换相关联的数值 'string:

 string s = Convert.ToInt32(cmd.login).ToString(); 

我也想知道为什么你实际上需要在你的代码中执行此操作;您可以以许多有用的方式使用枚举值,而无需将它们转换为任何其他类型;例如在Switch / Case块中:

 cmd cmd1 = cmd.login; 

switch (cmd1)
{
case cmd.login:
// 为'login 做任何事情
断裂;
case cmd.logout:
// 为'注销做任何事情
break ;
}


 使用系统; 
命名空间测试
{
public class 计划
{

private static void Main( string [] args)
{
int a =( int )cmd.login;
Console.ReadLine();
}
}
public 枚举 cmd {login = 11 ,logout = 12 };

}


当你声明这样的枚举时,= 11用于表示第一个项的值,然后其余的增量从那里开始。



尝试



  public   enum  cmd {login =  11 ,logout}; 


i wrote public enum cmd{login=11,logout=12};
i want to use those value when i wrie string a=cmd.login it takes logout but i want login value which is 11 help me?

解决方案

If you really want the numeric value associated with the Enum 'cmd converted to a 'string:

string s = Convert.ToInt32(cmd.login).ToString();

I, also, wonder why you would actually need to do this in your code; you can use Enum values in many useful ways without converting them to any other Type; for example in a Switch/Case block:

cmd cmd1 = cmd.login;

switch (cmd1)
{
    case cmd.login:
        // do whatever for 'login
        break;
    case cmd.logout:
        // do whatever for 'logout
        break;
}


using System;
namespace Test
{
   public class Program
    {

       private static void Main(string[] args)
       {
           int a = (int)cmd.login;
           Console.ReadLine();
       }
    }
   public enum cmd { login = 11, logout = 12 };

}


When you declare an enum like that, the =11 is used to denote the value of the first item, then the rest increment from there.

Try

public enum cmd{login=11,logout};


这篇关于枚举问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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