Reputation: 41208
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.
Upvotes: 1
Views: 2519
Reputation: 100381
You can't directly hold a string, but you can assign string values with annotations and retrieve using reflection.
Upvotes: 0
Reputation: 5924
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;
}
Upvotes: 2
Reputation: 52371
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.
Upvotes: 3
Reputation: 4009
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.
Upvotes: 0
Reputation: 72015
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.
Upvotes: 2