将字符串值转换为枚举值 C#

发布于 2024-10-09 21:51:36 字数 641 浏览 0 评论 0 原文

我有一个类 MyClass ,它有

public enum Days{Mon, Tue, Wed}

一个字段

public Days dayOfWeek;

,来自我的解决方案中的另一个类,我有一个字符串 (myString) 值 012。我想将 MyClass 的实例(称为 myClassInstance)字段 dayOfWeek 设置为等于 myStringvalue,这样 < code>0 表示星期一,1 表示星期二...

我已经尝试过

myClassInstance.dayOfWeek = Convert.ToInt32(myString)

myClassInstance.dayOfWeek = (int) myString

但都不起作用。我确信这很简单。为什么这些技术不起作用?

I have a class MyClass which has

public enum Days{Mon, Tue, Wed}

and then a field

public Days dayOfWeek;

From another class in my solution I have a string (myString) value of either 0, 1 or 2. I want to set an instance of MyClass's (called myClassInstance) field dayOfWeek equal to myStringvalue such that 0 means Mon, 1 means Tue...

I have tried

myClassInstance.dayOfWeek = Convert.ToInt32(myString)

and

myClassInstance.dayOfWeek = (int) myString

but neither work. I'm sure this is straightforward. Why don't these techniques work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

旧时模样 2024-10-16 21:51:36

尝试

string s = "0";
Days day = (Days)Enum.Parse(typeof(Days), s);

string s = "0";
Days day;
if(!Enum.TryParse(s, out day)) {
    // error handling
}

优雅地处理 s 无法解析为 Days 实例的情况。

这适用于 Enum.Parse 其中指出

将一个或多个枚举常量的名称或数字值的字符串表示形式转换为等效的枚举对象


此外,您可以通过 Enum.Parse 检查 string 的实例实际上是否表示由枚举定义的值,

string s = "3";
bool defined = Enum.IsDefined(typeof(Days), s);
// defined is false

在这种情况下将盲目解析 s ,即使它不表示由枚举 Days 定义的值,并且从 Enum.Parse 的结果到 Days 的转换也不会失败。

此外,还有一个内置枚举代表一周中的几天。此枚举是 System.DayOfWeek 。我建议使用这个。

最后,如果由于某种原因您无法使用 System.DayOfWeek,您至少应该将枚举重命名为 Day 而不是 Days (去掉复数形式)。只有代表标志的枚举才应该是复数的。注意上面的变量day代表的是一天,并不代表天。这就是为什么您应该将枚举重命名为 Day。这符合命名约定大多数 C# 和 .NET 程序员使用的。

Try

string s = "0";
Days day = (Days)Enum.Parse(typeof(Days), s);

or

string s = "0";
Days day;
if(!Enum.TryParse(s, out day)) {
    // error handling
}

to gracefully handle the case where s can't be parsed to an instance of Days.

This works per the documentation for Enum.Parse which states

Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

Additionally, you can check if the instance of string actually represents a value defined by the enum via

string s = "3";
bool defined = Enum.IsDefined(typeof(Days), s);
// defined is false

Enum.Parse will blindly parse s in this case, even though it doesn't represent a value defined by the enum Days and the cast from the result of Enum.Parse to Days 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 to Day instead of Days (remove the pluralization). Only enums that represent flags should be pluralized. Note that the variable day above represents a day, and it does not represent days. This is why you should rename the enum to Day. This is consistent with the naming conventions that most C# and .NET programmers use.

残疾 2024-10-16 21:51:36

您只需在转换后将 int 转换为 Days 枚举即可:

myClassInstance.dayOfWeek = (Days)Convert.ToInt32(myString);

您还可以使用 Enum.TryParse(如果您使用的是 .NET 4)或 Enum.Parse。根据您对传入数据的信任程度,您可能还需要调用 Enum.IsDefined 以确保整数是 Days 的有效值(否则,在所有这些情况下,您将拥有一个实例 与您的任何命名值都不对应)。

Days dayOfWeek;
if (!Enum.TryParse(myString, out dayOfWeek)) {
    dayOfWeek = Days.Mon; // or some other default, or throw
}

myClassInstance.dayOfWeek = dayOfWeek;

或者

myClassInstance.dayOfWeek = (Days)Enum.Parse(typeof(Days), myString);

此外,正如其他人提到的,您可能需要考虑使用内置的 DayOfWeek 枚举而不是您的自定义版本(如果它符合您真正想要的)。

另外,正如其他人再次提到的,即使没有,根据 .NET 命名指南,Day 是一个更好的名称,因为它不是 Flags 枚举。

You just need to cast the int to the Days enum after converting it:

myClassInstance.dayOfWeek = (Days)Convert.ToInt32(myString);

You can also use Enum.TryParse (if you're in .NET 4) or Enum.Parse. Depending on how much your trust the incoming data, you may also want to call Enum.IsDefined to make sure that the integer is a valid value of Days (otherwise, in all of these cases, you'll have an instance of Days that doesn't correspond to any of your named values).

Days dayOfWeek;
if (!Enum.TryParse(myString, out dayOfWeek)) {
    dayOfWeek = Days.Mon; // or some other default, or throw
}

myClassInstance.dayOfWeek = dayOfWeek;

Or

myClassInstance.dayOfWeek = (Days)Enum.Parse(typeof(Days), myString);

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 a Flags enum.

薄暮涼年 2024-10-16 21:51:36

尝试

myClassInstance.dayOfWeek = (Days)int.Parse(myString);

您是否知道已经有一个可以重复使用的名为 DayOfWeek 的枚举 (http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx)

Try

myClassInstance.dayOfWeek = (Days)int.Parse(myString);

Do you know there is already an Enum that you can resuse called DayOfWeek (http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx)

苏别ゝ 2024-10-16 21:51:36

尝试:

Days d = (Days)Enum.Parse(typeof(Days), myString);

此处有一个更完整的示例。

Try:

Days d = (Days)Enum.Parse(typeof(Days), myString);

A more complete example is here.

猫卆 2024-10-16 21:51:36

您需要将 int 转换为 Days

myClassInstance.dayOfWeek = (Days)Convert.ToInt32(myString);

You need to cast the int to Days:

myClassInstance.dayOfWeek = (Days)Convert.ToInt32(myString);
过气美图社 2024-10-16 21:51:36

您想要做的是将字符串转换为 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);

空气里的味道 2024-10-16 21:51:36

这应该有效

myClassInstance.dayOfWeek = Enum.Parse(typeof(Days), myString);

Enump.Parse< /a>

This should work

myClassInstance.dayOfWeek = Enum.Parse(typeof(Days), myString);

Enump.Parse

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文