Enumeration iteration

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

    Enumeration iteration

    Hi

    This has been irritating me for a while, so I think its not possible,
    but hopefully Im wrong...

    Is it possible to iterate an enum ? I want to do something like this.

    for each enum in enumname
    'do something (eg add item to select box)
    next

    thanks,

    Matthew
  • Armin Zingler

    #2
    Re: Enumeration iteration

    "Matthew" <matthew_glen_e vans@hotmail.co m> schrieb
    [color=blue]
    > This has been irritating me for a while, so I think its not
    > possible, but hopefully Im wrong...
    >
    > Is it possible to iterate an enum ? I want to do something like
    > this.
    >
    > for each enum in enumname
    > 'do something (eg add item to select box)
    > next[/color]


    Dim Name As String
    Dim Names As String()

    Names = [Enum].GetNames(GetTy pe(FormBorderSt yle))

    For Each Name In Names
    Next



    --
    Armin

    Dieser Text beschreibt allgemeine Zitiergewohnheiten im Usenet. Die dabei gemachten Aeusserungen und Erlaeuterungen basieren dabei auf Erfahrungen des Autors. Wie zitiert man im Usenet? Zitieren lernen!





    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Enumeration iteration

      * matthew_glen_ev ans@hotmail.com (Matthew) scripsit:[color=blue]
      > This has been irritating me for a while, so I think its not possible,
      > but hopefully Im wrong...
      >
      > Is it possible to iterate an enum ? I want to do something like this.
      >
      > for each enum in enumname
      > 'do something (eg add item to select box)
      > next[/color]

      'Enum.GetNames' , 'Enum.GetValues '.

      --
      Herfried K. Wagner
      MVP ยท VB Classic, VB.NET
      <http://www.mvps.org/dotnet>

      Improve your quoting style:
      <http://learn.to/quote>
      <http://www.plig.net/nnq/nquote.html>

      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: Enumeration iteration

        Matthew,
        If you want values instead of names (Armin showed you names) try:

        Dim value As FormBorderStyle
        Dim values As FormBorderStyle ()

        values = DirectCast([Enum].GetValues(GetT ype(FormBorderS tyle)),
        FormBorderStyle ())

        For Each value In values
        Next

        Also, System.Enum (the base type for all enums) have a number of other
        useful shared functions, such as Format, Parse, IsDefined, and ToObject.
        Remember because Enum is a keyword, you need to escape [Enum] in square
        brackets to use the System.Enum type.

        Hope this helps
        Jay

        "Matthew" <matthew_glen_e vans@hotmail.co m> wrote in message
        news:4f49e314.0 310300758.19c61 41@posting.goog le.com...[color=blue]
        > Hi
        >
        > This has been irritating me for a while, so I think its not possible,
        > but hopefully Im wrong...
        >
        > Is it possible to iterate an enum ? I want to do something like this.
        >
        > for each enum in enumname
        > 'do something (eg add item to select box)
        > next
        >
        > thanks,
        >
        > Matthew[/color]


        Comment

        • Matthew

          #5
          Re: Enumeration iteration

          "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in message news:<OpiN61wnD HA.1656@tk2msft ngp13.phx.gbl>. ..[color=blue]
          > Matthew,
          > If you want values instead of names (Armin showed you names) try:
          >
          > Dim value As FormBorderStyle
          > Dim values As FormBorderStyle ()
          >
          > values = DirectCast([Enum].GetValues(GetT ype(FormBorderS tyle)),
          > FormBorderStyle ())
          >
          > For Each value In values
          > Next
          >
          > Also, System.Enum (the base type for all enums) have a number of other
          > useful shared functions, such as Format, Parse, IsDefined, and ToObject.
          > Remember because Enum is a keyword, you need to escape [Enum] in square
          > brackets to use the System.Enum type.
          >
          > Hope this helps
          > Jay
          >
          > "Matthew" <matthew_glen_e vans@hotmail.co m> wrote in message
          > news:4f49e314.0 310300758.19c61 41@posting.goog le.com...[color=green]
          > > Hi
          > >
          > > This has been irritating me for a while, so I think its not possible,
          > > but hopefully Im wrong...
          > >
          > > Is it possible to iterate an enum ? I want to do something like this.
          > >
          > > for each enum in enumname
          > > 'do something (eg add item to select box)
          > > next
          > >
          > > thanks,
          > >
          > > Matthew[/color][/color]


          Thanks very much Jay and Armin.

          Comment

          • Matthew

            #6
            Re: Enumeration iteration

            "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in message news:<OpiN61wnD HA.1656@tk2msft ngp13.phx.gbl>. ..[color=blue]
            > Matthew,
            > If you want values instead of names (Armin showed you names) try:
            >
            > Dim value As FormBorderStyle
            > Dim values As FormBorderStyle ()
            >
            > values = DirectCast([Enum].GetValues(GetT ype(FormBorderS tyle)),
            > FormBorderStyle ())
            >
            > For Each value In values
            > Next
            >
            > Also, System.Enum (the base type for all enums) have a number of other
            > useful shared functions, such as Format, Parse, IsDefined, and ToObject.
            > Remember because Enum is a keyword, you need to escape [Enum] in square
            > brackets to use the System.Enum type.
            >
            > Hope this helps
            > Jay
            >
            > "Matthew" <matthew_glen_e vans@hotmail.co m> wrote in message
            > news:4f49e314.0 310300758.19c61 41@posting.goog le.com...[color=green]
            > > Hi
            > >
            > > This has been irritating me for a while, so I think its not possible,
            > > but hopefully Im wrong...
            > >
            > > Is it possible to iterate an enum ? I want to do something like this.
            > >
            > > for each enum in enumname
            > > 'do something (eg add item to select box)
            > > next
            > >
            > > thanks,
            > >
            > > Matthew[/color][/color]


            For anyone interested here is how I have implemented the population of
            a selectbox from an enum, using the advice of Jay and Armin: (XKeyType
            is the Enum)

            Dim val As XKeyType
            Dim enumvals As XKeyType() =
            DirectCast([Enum].GetValues(GetT ype(XKeyType)), XKeyType())
            Dim li As ListItem
            For Each val In enumvals
            li = New ListItem
            With li
            .Value = val
            .Text = [Enum].Format(GetType (XKeyType), val, "g")
            End With
            ddlist1.Items.A dd(li)
            Next

            Comment

            Working...