枚举可以只保存数字值而不保存字符串吗?

发布于 2024-09-08 23:06:02 字数 1558 浏览 2 评论 0原文

我有这个枚举:

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 技术交流群。

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

发布评论

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

评论(6

百合的盛世恋 2024-09-15 23:06:02

我建议使用 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.

养猫人 2024-09-15 23:06:02

是的,你不能那样做。枚举值仅是数字。考虑定义一个将枚举值映射到字符串/字符值的静态结构,或者将枚举更改为具有与每个枚举值对应的预定义静态实例的类。

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.

凉薄对峙 2024-09-15 23:06:02

您对使用枚举有什么特别的执着吗?如果没有,您可以通过使用带有常量的静态类来近似您想要的内容:

public static class AsciiChars
{
    public const string Black = "@";
    public const string Charcoal = "#";
    ...
    public const string White = " ";
}

然后您可以像在示例代码中指定的那样使用这些值:

string ascii;
if (redValue >= 230)
{
    ascii = AsciiChars.White;
}

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:

public static class AsciiChars
{
    public const string Black = "@";
    public const string Charcoal = "#";
    ...
    public const string White = " ";
}

You could then use these values just as you've specified in your sample code:

string ascii;
if (redValue >= 230)
{
    ascii = AsciiChars.White;
}
寂寞陪衬 2024-09-15 23:06:02

您在帖子标题中提出的问题的答案是:是的。

参见 http://msdn.microsoft.com/en-us/library/sbbt4032 .aspx

每个枚举类型都有一个基础类型,它可以是除 char 之外的任何整型。枚举元素的默认基础类型是 int。

The answer to your question in the thread's title is: Yes.

Cf http://msdn.microsoft.com/en-us/library/sbbt4032.aspx:

Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of the enumeration elements is int.

筑梦 2024-09-15 23:06:02

使用 '@' 而不是 "@"

Use '@' rather than "@"

浮生面具三千个 2024-09-15 23:06:02

您不能直接保存字符串,但可以使用注释分配字符串值并使用反射进行检索。

看看这个

You can't directly hold a string, but you can assign string values with annotations and retrieve using reflection.

Have a look at this.

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