Quick question about enums
I was wondering...
enum Size : int
{
SIZE_SMALL = 0,
SIZE_MEDIUM = 1,
SIZE_BIG = 2
}
int myInt = 1;
string myString = "SIZE_SMALL";
Is there anyway I can access members in the enum "Size" using my 2 variables? (By value or by name)?
I don't want to just get the name, or get the value, I want a reference to that particular constant.
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Mike GoldRank #50 · 30.6K pointsPosted Jan 3, 2006, 10:22 AM EST
Check the code shown below. s1 shows you how to get the constant from a string. s2 shows you how to get the constant from an integer:
private void ShowEnum() { Size s1 = (Size)Enum.Parse(typeof(Size), "SIZE_SMALL", true); Size s2 = (Size)myInt; Console.WriteLine("s1={0}", s1); Console.WriteLine("s2={0}", s2); }1 short reply
Brian3 pointsPosted Jan 3, 2006, 10:26 AM EST