Getting Enum Reference

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

    #1

    Getting Enum Reference

    Say that I create a combobox with pull down items that map directly to an
    enum. For example the dropdown items could be "Up", "Down", "Left" and
    "Right" and the enum could be declare as: public enum DirectionEnum {Up,
    Down, Left, Right};

    If I needed to get an enum reference based on the dropdown selection, as of
    today I would do the following:

    DirectionEnum myEnum; // Declare enum.
    If(someCombobox .Text == "Up") myEnum == DirectionEnum.U p;
    If(someCombobox .Text == "Down") myEnum == DirectionEnum.D own;
    etc...

    What I would like to do is something like my make believe following code:
    myEnum = DirectionEnum.G etEnumByName(so meCombobox.Text );

    Is there any way to achieve something like the code above?

    Thank you.


  • Jon Skeet [C# MVP]

    #2
    Re: Getting Enum Reference

    Rene <nospam@nospam. nospam> wrote:[color=blue]
    > Say that I create a combobox with pull down items that map directly to an
    > enum. For example the dropdown items could be "Up", "Down", "Left" and
    > "Right" and the enum could be declare as: public enum DirectionEnum {Up,
    > Down, Left, Right};
    >
    > If I needed to get an enum reference based on the dropdown selection, as of
    > today I would do the following:
    >
    > DirectionEnum myEnum; // Declare enum.
    > If(someCombobox .Text == "Up") myEnum == DirectionEnum.U p;
    > If(someCombobox .Text == "Down") myEnum == DirectionEnum.D own;
    > etc...
    >
    > What I would like to do is something like my make believe following code:
    > myEnum = DirectionEnum.G etEnumByName(so meCombobox.Text );
    >
    > Is there any way to achieve something like the code above?[/color]

    I think you're looking for:

    myEnum = (DirectionEnum) Enum.Parse (typeof(Directi onEnum),
    someCombobox.Te xt);

    --
    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

    • Rene

      #3
      Re: Getting Enum Reference

      Excellent!!

      Thanks.



      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
      news:MPG.1a326d 9a1f90cf21989b6 e@msnews.micros oft.com...[color=blue]
      > Rene <nospam@nospam. nospam> wrote:[color=green]
      > > Say that I create a combobox with pull down items that map directly to[/color][/color]
      an[color=blue][color=green]
      > > enum. For example the dropdown items could be "Up", "Down", "Left" and
      > > "Right" and the enum could be declare as: public enum DirectionEnum {Up,
      > > Down, Left, Right};
      > >
      > > If I needed to get an enum reference based on the dropdown selection, as[/color][/color]
      of[color=blue][color=green]
      > > today I would do the following:
      > >
      > > DirectionEnum myEnum; // Declare enum.
      > > If(someCombobox .Text == "Up") myEnum == DirectionEnum.U p;
      > > If(someCombobox .Text == "Down") myEnum == DirectionEnum.D own;
      > > etc...
      > >
      > > What I would like to do is something like my make believe following[/color][/color]
      code:[color=blue][color=green]
      > > myEnum = DirectionEnum.G etEnumByName(so meCombobox.Text );
      > >
      > > Is there any way to achieve something like the code above?[/color]
      >
      > I think you're looking for:
      >
      > myEnum = (DirectionEnum) Enum.Parse (typeof(Directi onEnum),
      > someCombobox.Te xt);
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      Working...