I don´t really understand the use of enum. I guess its purpose is to simplify human understending of code, since it allows to replace sheer numbers with miningful words.
So I create an enum:
1 2 3 4 5 6 7
enum COLOR
{
BLACK,
WHITE,
RED,
//...
};
and now I can store information using BLACK as a value (0 for the computer), or RED (that would be 2).
My question is:
Can I get the word RED by providing number two?
Can I ask the computer to show me the actual word?
I think you meant character literals instead of words.
The answer is no. These names are known only at compilation time. What you are saying about is present in C#.
For example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
using System;
namespace TestEnum
{
enum Color { Red, Green, Blue };
class Program
{
staticvoid Main(string[] args)
{
var c = Color.Red;
Console.WriteLine("{0:d} is {0}",c);
}
}
}
For the sake of this argument, think of an enum as a set of integers. You can't output the name you chose for the variable in the program because that doesn't exist after compile time. So no, you can't output the actual word in that sense.
using System;
namespace TestEnum
{
enum Color { Red, Green, Blue };
class Program
{
staticvoid Main(string[] args)
{
Array enumData = Enum.GetValues( typeof( Color ) );
for ( int i = 0; i < enumData.Length; i++ )
{
Console.WriteLine("{0:d} is {0}", enumData.GetValue(i));
}
}
}
}
Now I get it better, specially from that old forum thread. I was lacking good examples of its use, and I was trying also to have the user input the enumeration values straightforward. Thank you, Thumper!