Enum value by string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Davis

    #1

    Enum value by string?

    What's the easiest and cleanest way to fetch an enum value by its name?

    For instance,

    public enum Boy {
    Bob, Billy, Joe
    }

    public Boy GetBoy(string boyName) {
    return ?? // and no, I refuse to use switch/case.
    }

    Thanks,
    Jon


  • Jon Skeet

    #2
    Re: Enum value by string?

    Jon Davis <jon@REMOVE.ME. PLEASE.jondavis .net> wrote:[color=blue]
    > I think I found it. Enum.Parse(), right?[/color]

    Yup. Enum.Parse (typeof(Boy), "Bob");

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    Working...