将字符串值转换为枚举值 C#
我有一个类 MyClass
,它有
public enum Days{Mon, Tue, Wed}
一个字段
public Days dayOfWeek;
,来自我的解决方案中的另一个类,我有一个字符串 (myString
) 值 0
, 1
或 2
。我想将 MyClass
的实例(称为 myClassInstance
)字段 dayOfWeek
设置为等于 myStringvalue
,这样 < code>0 表示星期一,1
表示星期二...
我已经尝试过
myClassInstance.dayOfWeek = Convert.ToInt32(myString)
,
myClassInstance.dayOfWeek = (int) myString
但都不起作用。我确信这很简单。为什么这些技术不起作用?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试
或
优雅地处理
s
无法解析为Days
实例的情况。这适用于
Enum.Parse
其中指出此外,您可以通过
Enum.Parse
检查string
的实例实际上是否表示由枚举定义的值,在这种情况下将盲目解析
s
,即使它不表示由枚举Days
定义的值,并且从Enum.Parse
的结果到Days
的转换也不会失败。此外,还有一个内置枚举代表一周中的几天。此枚举是
System.DayOfWeek
。我建议使用这个。最后,如果由于某种原因您无法使用
System.DayOfWeek
,您至少应该将枚举重命名为Day
而不是Days
(去掉复数形式)。只有代表标志的枚举才应该是复数的。注意上面的变量day
代表的是一天,并不代表天。这就是为什么您应该将枚举重命名为Day
。这符合命名约定大多数 C# 和 .NET 程序员使用的。Try
or
to gracefully handle the case where
s
can't be parsed to an instance ofDays
.This works per the documentation for
Enum.Parse
which statesAdditionally, you can check if the instance of
string
actually represents a value defined by the enum viaEnum.Parse
will blindly parses
in this case, even though it doesn't represent a value defined by the enumDays
and the cast from the result ofEnum.Parse
toDays
will not fail.Moreover, there is a built-in enum that represents the days of the week. This enum is
System.DayOfWeek
. I would suggest using this.Finally, if for some reason you can't use
System.DayOfWeek
, you should at a minimum rename your enum toDay
instead ofDays
(remove the pluralization). Only enums that represent flags should be pluralized. Note that the variableday
above represents a day, and it does not represent days. This is why you should rename the enum toDay
. This is consistent with the naming conventions that most C# and .NET programmers use.您只需在转换后将
int
转换为Days
枚举即可:您还可以使用
Enum.TryParse
(如果您使用的是 .NET 4)或Enum.Parse
。根据您对传入数据的信任程度,您可能还需要调用Enum.IsDefined
以确保整数是Days
的有效值(否则,在所有这些情况下,您将拥有一个实例天
与您的任何命名值都不对应)。或者
此外,正如其他人提到的,您可能需要考虑使用内置的
DayOfWeek
枚举而不是您的自定义版本(如果它符合您真正想要的)。另外,正如其他人再次提到的,即使没有,根据 .NET 命名指南,
Day
是一个更好的名称,因为它不是Flags
枚举。You just need to cast the
int
to theDays
enum after converting it:You can also use
Enum.TryParse
(if you're in .NET 4) orEnum.Parse
. Depending on how much your trust the incoming data, you may also want to callEnum.IsDefined
to make sure that the integer is a valid value ofDays
(otherwise, in all of these cases, you'll have an instance ofDays
that doesn't correspond to any of your named values).Or
In addition, as others have mentioned, you may want to consider using the built-in
DayOfWeek
enum instead of your custom version, if it matches what you really want.Also, as others have mentioned again, even if it doesn't,
Day
is a better name based on the .NET naming guidelines, since it isn't aFlags
enum.尝试
您是否知道已经有一个可以重复使用的名为
DayOfWeek
的枚举 (http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx)Try
Do you know there is already an Enum that you can resuse called
DayOfWeek
(http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx)尝试:
此处有一个更完整的示例。
Try:
A more complete example is here.
您需要将
int
转换为Days
:You need to cast the
int
toDays
:您想要做的是将字符串转换为 int (如果您想以更好的方式执行此操作,则尝试解析)
然后使用以下代码设置枚举值:
(Days)Enum.ToObject(typeof(Days), intValue );
What you want to do is cast the string to an int (or tryparse if you want to do it in a nice way)
Then use the Following code to set the enum value:
(Days)Enum.ToObject(typeof(Days), intValue);
这应该有效
Enump.Parse< /a>
This should work
Enump.Parse