Skip to content

Quick question about enums

Brian

Asked Jan 3, 2006, 9:13 AM EST2 answers
Share
LinkedInX
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 Gold
    0
    Hi Brian,

    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:

    C#
    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);
    }
    0
  • 1 short reply
    • 0
      Thanks :) Works great!
      0