准备条件:

①枚举类型:

     public enum enumColor
{
Red = ,
Yellow,
Green,
Blue,
White,
Black
}

②以下状态都是理想状态,并未对错误数据进行处理。

1.枚举类型转换为字符串

        private string EnumConvertToString(enumColor color)
{
//方法一
//return color.ToString(); //方法二
return Enum.GetName(color.GetType(), color);

2.枚举类型转换为数字

        private int EnumConvertToInt(enumColor color)
{
return (int)color;
}

3.字符串转换为枚举类型

        private enumColor StringConvertToEnum(string str)
{
enumColor color = enumColor.Red;
try
{
color = (enumColor)Enum.Parse(typeof(enumColor), str);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return color;
} return color;
}

4.数字转换为枚举类型

        private enumColor IntConvertToEnum(int i)
{
if (Enum.IsDefined(typeof(enumColor), i))
{
return (enumColor)Enum.ToObject(typeof(enumColor), i);
}
return enumColor.Red;
}

        private enumColor IntConvertToEnumOther(int i)
{
return (enumColor)i;
}

附件为一个Demo小工具:

http://files.cnblogs.com/files/wangyblzu/EnumConvertDemo.zip

【C#】枚举和字符串以及数字之间的互相转换的更多相关文章

  1. js 获取小数点位数方法及 字符串与数字之间相互转换方法

    1.获取小数点位数方法 a. 使用 js 中 subsrting,indexOf,parseFloat三个函数,代码如下: var s = "22.127456" ;//s 为 字 ...

  2. kotlin字符串和数字之间的转换和人机交互

    继续基础学习~ 字符串和数字之间的转换 那如何转换呢,其实很简单: 编译木有报错,但是运行: 所以这里了解下. 人机交互 看这标题貌似高端的,其实也就是程序可以接受键盘的输入啦,下面开始: 首先提示用 ...

  3. js字符串与数字之间的比较

    //1.纯数字之间比较 console.log(1<3);//true //2.纯字符串比较,先转成ASCII码,按位依次比较 console.log("1"<&quo ...

  4. js中字符串,数字之间转换的常用方法

    var number={ num:, num1:"2你好" }; //将数字转换为字符串 var str=number.num.toString();//十进制 );//二进制 ) ...

  5. C/C++中字符串与数字之间的转换

    主要有两种方式:C 中能够使用 sprintf 将数字转为字符数组,sscanf 将字符数组转为数字:而在 C++ 中不仅能够使用 C 中的方法,还能够使用 stringstream 实现字符串与数字 ...

  6. python 字符串与数字之间的转换

    1.数字转字符串 i = 123 str = ‘%d’ %i str即为转换成的字符串 2.字符串转换成数字: import string tt='555' ts=string.atoi(tt) ts ...

  7. java中的字符,字符串,数字之间的转换

    string 和int之间的转换 string转换成int  :Integer.valueOf("12") int转换成string : String.valueOf(12) ch ...

  8. java中的字符、字符串及数字之间的转换(转)

    一.string 和int之间的转换 1.string转换成int  :Integer.valueOf("12") 2.int转换成string : String.valueOf( ...

  9. java字符,字符串,数字之间的转换

    string 和int之间的转换 string转换成int  :Integer.valueOf("12") int转换成string : String.valueOf(12) ch ...

随机推荐

  1. 修改DedeCMS图片上传路径命名规则的具体方法步骤

    收藏到:0时间:2013-08-23   文章来源:马海祥博客   访问次数:2350 最近在整理网站根目录下文件的时候,发现马海祥博客网站已经有上千个文件夹了,其中光图片文件夹就占了近一半.这个主要 ...

  2. Babel6.x的安装

    1.首先安装babel-cli(用于在终端使用babel) npm install -g babel-cli 2.然后安装babel-preset-es2015插件 npm install --sav ...

  3. spring-boot集成swagger

    1.引入swagger需要的java类库 <dependency> <groupId>io.springfox</groupId> <artifactId&g ...

  4. Spring的AOP细节理解

    什么是AOP?AOP:是面向切面编程,是对面向对象编程(oop)的一种补充,为什么需要AOP?例如在我们做一个计算器,要求我们每次运行对应的功能(也就是进行运算时)都要输出日志,以便于知道程序是怎么运 ...

  5. ajax跨域问题解决之cors篇

    现在浏览器出于安全考虑,在域名.协议.端口不同的情况下,浏览器会认为这是跨域,ajax请求是不允许跨域的. 如果我们有跨域的需求,可以使用cors解决.其原理就是,在请求之前先发送一个OPTIONS请 ...

  6. Rete_algorithm

    https://en.wikipedia.org/wiki/Rete_algorithm https://en.wikipedia.org/wiki/Rete_algorithm The Rete a ...

  7. 剖析Docker文件系统:Aufs与Devicemapper

    http://www.infoq.com/cn/articles/analysis-of-docker-file-system-aufs-and-devicemapper Docker镜像 典型的Li ...

  8. centos 7.5 最小化安装

    参考:https://www.tecmint.com/centos-7-installation/ ================================================== ...

  9. King's Game---hdu5643(约瑟夫环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5643    约瑟夫环问题的原来描述为,设有编号为1,2,……,n的n(n>0)个人围成一个圈,从 ...

  10. DtypeWarning: Columns (1,5,7,16,......) have mixed types. Specify dtype option on import or set low_memory=False.

    DtypeWarning: Columns (1,5,7,16,......) have mixed types. Specify dtype option on import or set low_ ...