将字符串类型转换为枚举类型时遇到的问题 [英] facing problem in converting string type to enum type

查看:73
本文介绍了将字符串类型转换为枚举类型时遇到的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我叫rakesh,是jr.software开发人员.

我遇到问题,如果有人可以解决这个问题,请帮帮我.''

我希望将字符串类型转换为枚举类型,以便可以在各种方法中将其用作枚举类型.

Hello ,

My name is rakesh , working as jr.software developer .

I Having an issue , If anybody can solve this please help me.''

I want the string type to be converted as enum type , so that i can use it as enum type in various methods .

推荐答案

Try:
Try:
Dim today As DayOfWeek = DirectCast([Enum].Parse(GetType(DayOfWeek), "Monday"), DayOfWeek)


为什么不这样做"您将为每个信息创建字典.键可以是枚举字符串表示形式,而值可以是枚举值.

这样,您可以将每个字典中的所有键添加到组合框中.一旦用户使用任何值,您只需要将实际枚举值作为dictionary["selectedstring"]传递即可,这将是一个枚举值.

这样,您也具有所有类型的安全性.

我希望我理解了这个问题,我的建议是有道理的.
Why don''t you create a dictionary for each information. The key could be the enum string representations and the value could be the enum value.

That way you can add all the keys from each dictionary into the combo boxes. once the user seelct any value use this you just have to pass the actual enum value as dictionary["selectedstring"] which will be a enum value.

This way you have all the type safety too.

I hope I understood the problem and my suggestion makes sense. if not correct my understanding please.


这个问题并不像看起来的那么简单.当为不同的枚举成员分配相同的整数值时,就会出现此问题.它会产生难以处理的歧义.

我对这个问题有一个全面的解决方案.在我的CodeProject的两篇文章中对此进行了解释:
​​枚举类型不枚举!解决.NET和语言限制 [ ^ ],
人类可读的枚举元数据 [ ^ ].

这是我所知道的唯一全面的解决方案.请参见.您可以找到可以使用的许多相关技术(甚至更简单的技术)的说明.

—SA
This question is not so simple as it may seem. The problem is created by the cases when the different enumeration members are assigned the same integer values. It creates ambiguity which is hard to handle.

I have a comprehensive solution of this problem. It is explained in two of my CodeProject articles:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^],
Human-readable Enumeration Meta-data[^].

This is the only comprehensive solution I know. Please see. You can find explanation of a number of related techniques (even simpler ones) you could use.

—SA


这篇关于将字符串类型转换为枚举类型时遇到的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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