enum string value

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

    enum string value

    I have string value, and I want to check if enum contains it..

    Say I have enum MyTest {
    Foo=1,
    Bar
    }

    and is using pass string argument="Foo"; I'd like to check if it in
    the enum.

    Now I do the following, but I suspect there is an easier way:

    foreach (MyTest t in Enum.GetValues( typeof(MyTest )))
    {
    if (argument== t.ToString())
    {
    //DO SOMETHING
    }
    }

    Thanks
  • Marc Gravell

    #2
    Re: enum string value

    Well, you could use Enum.Parse(), or Enum.IsDefined( ) - although note
    that it has been suggested that Enum.IsDefined( ) should be avoided. I
    haven't made up my mind on the subject yet... but see here:


    Marc

    Comment

    • Jeff Johnson

      #3
      Re: enum string value

      "Marc Gravell" <marc.gravell@g mail.comwrote in message
      news:2bea6281-a77a-4255-9987-e6b3911264d8@k3 7g2000hsf.googl egroups.com...
      I shudder to think what the color "Ultraviola te" looks like....


      Comment

      • Marc Gravell

        #4
        Re: enum string value

        I shudder to think what the color "Ultraviola te" looks like....

        lol - sounds like a title from the "somewhat dubious" section of the
        video store...

        Marc

        Comment

        Working...