Checking an ENUM

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

    #1

    Checking an ENUM

    Hello all,

    Is there a way to loop through a ENUM, like using a foreach loop?

    I'd also like to know if you can do a 'Contains' or like method on an ENUM.

    Thanks all,

    JY
  • Mattias Sjögren

    #2
    Re: Checking an ENUM

    >Is there a way to loop through a ENUM, like using a foreach loop?

    foreach (YourEnum e in Enum.GetValues( typeof(YourEnum )))

    [color=blue]
    >I'd also like to know if you can do a 'Contains' or like method on an ENUM.[/color]

    Enum.IsDefined( )


    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Michael Bray

      #3
      Re: Checking an ENUM

      =?Utf-8?B?Sm9u?= <Jon@discussion s.microsoft.com > wrote in
      news:88ADAEB4-DF25-4AEE-B4B5-D08C8F278334@mi crosoft.com:
      [color=blue]
      > Hello all,
      >
      > Is there a way to loop through a ENUM, like using a foreach loop?[/color]

      Take a look at Enum.GetValues( type), or Enum.GetNames(t ype).

      -mdb

      Comment

      • BC3Tech

        #4
        Re: Checking an ENUM

        since an enumeration is (usually) integer based. you can also do
        Enum.Parse(0), 1, 2, .... to get the values at the respective places
        until an exception is thrown.

        Comment

        Working...