tags:

views:

126

answers:

6

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.

+1  A: 

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.

mquander
A: 

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.

Andreas
+3  A: 

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.

MainMa
A: 

Use '@' rather than "@"

Mark H
+1  A: 

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;
}
Mike Powell
A: 

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

Have a look at this.

BrunoLM