惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
GbyAI
GbyAI
aimingoo的专栏
aimingoo的专栏
L
LangChain Blog
I
InfoQ
D
Docker
F
Fortinet All Blogs
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
月光博客
月光博客
B
Blog
Engineering at Meta
Engineering at Meta
T
Tailwind CSS Blog
罗磊的独立博客
博客园_首页
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
D
DataBreaches.Net
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog RSS Feed
IT之家
IT之家
V
V2EX

博客园 - 周伟

域名解析文件hosts文件是什么?如何修改hosts文件? PowerDesigner 使用心得 实现TOMCAT服务下一个ip绑定多域名绑定的方法 CSV扩展类 C++导入导出CSV文件 struct+vector实现树结构 WebService 随写 mysql相关文章收集 Linux下两种自动启动Tomcat的方法 linux自动启动mysql LINUX下mysql的大小写区分设置 Jdom使用指南 vector+struct在数据接口中的应用 UML用例中使用和扩展意义 单件模式的C++标准实现 设计模式 Add Crash Reporting to Your Applications with the CrashRpt Library 设计模式——单键模式(singleton)C++实现 Singleton的C++实现 及相关问题
枚举和字符串互转
周伟 · 2009-05-14 · via 博客园 - 周伟

C#

如果将一个枚举值转换为字符串就非常的简单,直接使用枚举的Tostring()就可以了。
那么将一个字符串转换成枚举可就有一点麻烦了。方法如下:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string s = "ss";
            dd p = (dd)Enum.Parse(typeof(dd),s);
            MessageBox.Show(p.ToString());
        }

    }
    public enum dd
    {
        mm,ss
    }

// This code requires the use of the System namespace.
enum Colors
{
red, green, blue
}
string colorString = "blue";
// Note that the Parse method below is a method defined by System.Enum,
// not by Colors.
Colors actualEnum = (Colors)Colors.Parse(typeof(Colors), colorString);
// actualEnum = blue

C++: