枚举可以只保存数字值而不保存字符串吗?
我有这个枚举:
enum ASCIIChars
{
BLACK = "@",
CHARCOAL = "#",
DARKGRAY = "8",
MEDIUMGRAY = "&",
MEDIUM = "o",
GRAY = ":",
SLATEGRAY = "*",
LIGHTGRAY = ".",
WHITE = " "
};
这是我使用它的地方:
private void GetShadeColor(int redValue)
{
string ASCII = " ";
if (redValue >= 230)
{
ASCII = ASCIIChars.WHITE;
}
else if (redValue >= 200)
{
ASCII = ASCIIChars.LIGHTGRAY;
}
else if (redValue >= 180)
{
ASCII = ASCIIChars.SLATEGRAY;
}
else if (redValue >= 160)
{
ASCII = ASCIIChars.GRAY;
}
else if (redValue >= 130)
{
ASCII = ASCIIChars.MEDIUM;
}
else if (redValue >= 100)
{
ASCII = ASCIIChars.MEDIUMGRAY;
}
else if (redValue >= 70)
{
ASCII = ASCIIChars.DARKGRAY;
}
else if (redValue >= 50)
{
ASCII = ASCIIChars.CHARCOAL;
}
else
{
ASCII = ASCIIChars.BLACK;
}
ASCIIArt.Append(ASCII);
}
我在枚举声明中收到以下错误:
无法将字符串隐式转换为 int。
I have this Enum:
enum ASCIIChars
{
BLACK = "@",
CHARCOAL = "#",
DARKGRAY = "8",
MEDIUMGRAY = "&",
MEDIUM = "o",
GRAY = ":",
SLATEGRAY = "*",
LIGHTGRAY = ".",
WHITE = " "
};
Here is where I am using it:
private void GetShadeColor(int redValue)
{
string ASCII = " ";
if (redValue >= 230)
{
ASCII = ASCIIChars.WHITE;
}
else if (redValue >= 200)
{
ASCII = ASCIIChars.LIGHTGRAY;
}
else if (redValue >= 180)
{
ASCII = ASCIIChars.SLATEGRAY;
}
else if (redValue >= 160)
{
ASCII = ASCIIChars.GRAY;
}
else if (redValue >= 130)
{
ASCII = ASCIIChars.MEDIUM;
}
else if (redValue >= 100)
{
ASCII = ASCIIChars.MEDIUMGRAY;
}
else if (redValue >= 70)
{
ASCII = ASCIIChars.DARKGRAY;
}
else if (redValue >= 50)
{
ASCII = ASCIIChars.CHARCOAL;
}
else
{
ASCII = ASCIIChars.BLACK;
}
ASCIIArt.Append(ASCII);
}
I'm getting the following errors in the enum declaration:
Cannot implicitly convert string to int.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我建议使用
Dictionary
代替。另一种方法是使用属性,但我不确定是否是对你的情况非常有帮助。
I would recommend using
Dictionary<AsciiCharacters, char>
instead.Another way is to use attributes, but I'm not sure if it is very helpful in your situation.
是的,你不能那样做。枚举值仅是数字。考虑定义一个将枚举值映射到字符串/字符值的静态结构,或者将枚举更改为具有与每个枚举值对应的预定义静态实例的类。
Yeah, you can't do that. Enum values are numeric-only. Consider defining a static structure mapping enum values to string/char values, or changing your enum to a class with a predefined static instance corresponding to each enum value.
您对使用枚举有什么特别的执着吗?如果没有,您可以通过使用带有常量的静态类来近似您想要的内容:
然后您可以像在示例代码中指定的那样使用这些值:
Do you have any special attachment to using an enum? If not, you can approximate what you want by using a static class with constants instead:
You could then use these values just as you've specified in your sample code:
您在帖子标题中提出的问题的答案是:是的。
参见 http://msdn.microsoft.com/en-us/library/sbbt4032 .aspx:
The answer to your question in the thread's title is: Yes.
Cf http://msdn.microsoft.com/en-us/library/sbbt4032.aspx:
使用
'@'
而不是"@"
Use
'@'
rather than"@"
您不能直接保存字符串,但可以使用注释分配字符串值并使用反射进行检索。
看看这个。
You can't directly hold a string, but you can assign string values with annotations and retrieve using reflection.
Have a look at this.