1

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.

Sergio Tapia
  • 40,006
  • 76
  • 183
  • 254
  • 2
    BTW, that is an awful way to convert color to monochrome. – Ed S. Jul 11 '10 at 01:02
  • 2
    @Ed - He's not converting to monochrome, it would appear he's mapping shades of red to ASCII characters for generating ASCII art. Do you have a better way of doing it? – Mike Powell Jul 11 '10 at 02:25

6 Answers6

3

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.

Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
2

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.

mqp
  • 70,359
  • 14
  • 95
  • 123
2

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
  • 5,914
  • 4
  • 28
  • 28
0

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,929
  • 2
  • 25
  • 22
0

Use '@' rather than "@"

Mark H
  • 13,797
  • 4
  • 31
  • 45
0

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

Have a look at this.

Community
  • 1
  • 1
BrunoLM
  • 97,872
  • 84
  • 296
  • 452