loop over Enum values

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

    loop over Enum values

    This seems like a long shot, but I wondered if there's a way to loop
    over the values in an Enum. For example, say I have an Enum as
    follows:

    Public Enum statusValues
    Ready
    Running
    Finished
    Error
    End Enum

    '.....

    I'd like to be able to loop over and print out these values' names
    something like this:

    For Each item in statusValues
    Console.WriteLi ne(item.ToStrin g)
    Next


    ....the reason being that, if I add elements to my Enum object, I won't
    have to go change the code that prints out their names. Thanks!

    --Frank
  • Roy Osherove

    #2
    Re: loop over Enum values

    On 25 Nov 2003 07:30:10 -0800, giant food wrote:
    [color=blue]
    > This seems like a long shot, but I wondered if there's a way to loop
    > over the values in an Enum. For example, say I have an Enum as
    > follows:
    >
    > Public Enum statusValues
    > Ready
    > Running
    > Finished
    > Error
    > End Enum
    >
    > '.....
    >
    > I'd like to be able to loop over and print out these values' names
    > something like this:
    >
    > For Each item in statusValues
    > Console.WriteLi ne(item.ToStrin g)
    > Next
    >
    > ...the reason being that, if I add elements to my Enum object, I won't
    > have to go change the code that prints out their names. Thanks!
    >
    > --Frank[/color]

    The Enum class has static methods that allow you to do this.
    For example, to get the name of a specific enum value that was passed to
    you, you could use
    string name = Enum.GetName(ty peof(MyEnumType ),SomeEnumValue )


    --
    Roy Osherove
    weblog: http://www.iserializable.com

    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: loop over Enum values

      Frank,
      Remember, all enums inherit from System.Enum, System.Enum has shared methods
      for retrieving information about Enums.

      Try something like:[color=blue]
      > For Each item in [Enum].GetValues(GetT ype(statusValue s))
      > Console.WriteLi ne(item.ToStrin g)
      > Next[/color]

      If you want the names of the Enum instead of the values, try:
      [color=blue]
      > For Each item in [Enum].GetNames(GetTy pe(statusValues ))
      > Console.WriteLi ne(item.ToStrin g)
      > Next[/color]

      The [Enum] is an escaped identifier it allows me to refer to the System.Enum
      type instead of the Enum keyword used to define an Enum.

      Other useful members of System.Enum include: Format, GetName, IsDefined,
      Parse, and ToObject.

      Hope this helps
      Jay

      "giant food" <dinosaur8000@y ahoo.com> wrote in message
      news:a61ab53c.0 311250730.3536c 737@posting.goo gle.com...[color=blue]
      > This seems like a long shot, but I wondered if there's a way to loop
      > over the values in an Enum. For example, say I have an Enum as
      > follows:
      >
      > Public Enum statusValues
      > Ready
      > Running
      > Finished
      > Error
      > End Enum
      >
      > '.....
      >
      > I'd like to be able to loop over and print out these values' names
      > something like this:
      >
      > For Each item in statusValues
      > Console.WriteLi ne(item.ToStrin g)
      > Next
      >
      >
      > ...the reason being that, if I add elements to my Enum object, I won't
      > have to go change the code that prints out their names. Thanks!
      >
      > --Frank[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: loop over Enum values

        * dinosaur8000@ya hoo.com (giant food) scripsit:[color=blue]
        > This seems like a long shot, but I wondered if there's a way to loop
        > over the values in an Enum. For example, say I have an Enum as
        > follows:
        >
        > Public Enum statusValues
        > Ready
        > Running
        > Finished
        > Error
        > End Enum
        >
        > '.....
        >
        > I'd like to be able to loop over and print out these values' names
        > something like this:
        >
        > For Each item in statusValues
        > Console.WriteLi ne(item.ToStrin g)
        > Next[/color]

        \\\
        Dim s As String
        For Each s In [Enum].GetNames(GetTy pe(KnownColor))
        Me.ListBox1.Ite ms.Add(s)
        Next s
        ///

        Replace 'KnownColor' with the name of your enum.

        --
        Herfried K. Wagner [MVP]
        <http://www.mvps.org/dotnet>

        Comment

        Working...