Enum

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

    #1

    Enum

    Hello,

    I have an enum:

    Public Enum Color
    Red
    Blue
    Green
    End Enum

    I need:

    1. Loop trough all enum items and get each one value (Ex: Red) and
    index (Ex: 1).

    For Each color As Color In [Enum].GetValues(GetT ype(Color))
    ???
    Next

    2. Get an enum value (Ex: Red) by providing its index (Ex: 1)

    I have been trying to find some info about this but no success.

    Thank you for your Help,

    Miguel

  • _AnonCoward

    #2
    Re: Enum


    "shapper" <mdmoura@gmail. comwrote in message
    news:1168635957 .974016.294280@ 11g2000cwr.goog legroups.com...
    : Hello,
    :
    : I have an enum:
    :
    : Public Enum Color
    : Red
    : Blue
    : Green
    : End Enum
    :
    : I need:
    :
    : 1. Loop trough all enum items and get each one value (Ex: Red)
    : and index (Ex: 1).
    :
    : For Each color As Color In [Enum].GetValues(GetT ype(Color))
    : ???
    : Next
    :
    : 2. Get an enum value (Ex: Red) by providing its index (Ex: 1)
    :
    : I have been trying to find some info about this but no success.
    :
    : Thank you for your Help,
    :
    : Miguel

    The following should do what you want. Bear in mind that Enum's are
    "value" types. In the code below, the System.Enum type's .Parse and
    ..GetNames functions perform "boxing" actions which are not
    particularly efficient. This code example below is deliberately
    designed to illustrate where this boxing and unboxing is occurring.

    '---------------------------------------
    'VB.NET 2.0
    '---------------------------------------
    Option Strict On
    Imports System

    Public Module [module]
    Public Enum Color
    Red
    Blue
    Green
    End Enum

    Public Sub Main

    'get a list of names for this enumeration
    For Each name As String In [Enum].GetNames(GetTy pe(Color))

    'convert the enumeration name into its value equivalent.
    Dim obj As Object = [Enum].Parse(GetType( Color), name)
    Dim value As Integer = CInt(obj)

    'display the name and its value
    Console.WriteLi ne(name & " = " & value)
    Next

    'get a list of the possible values for the enumeration
    For Each value As Integer In [Enum].GetValues(GetT ype(Color))

    'convert the value into its name equivalent
    Dim obj As Object = CObj(value)
    Dim name As String = [Enum].GetName(GetTyp e(Color), obj)

    'display the value and its name
    Console.WriteLi ne(value & " = " & name)
    Next

    End sub

    End Module
    '---------------------------------------

    Ralf
    --
    --
    ----------------------------------------------------------
    * ^~^ ^~^ *
    * _ {~ ~} {~ ~} _ *
    * /_``>*< >*<''_\ *
    * (\--_)++) (++(_--/) *
    ----------------------------------------------------------
    There are no advanced students in Aikido - there are only
    competent beginners. There are no advanced techniques -
    only the correct application of basic principles.


    Comment

    • shapper

      #3
      Re: Enum


      _AnonCoward wrote:
      "shapper" <mdmoura@gmail. comwrote in message
      news:1168635957 .974016.294280@ 11g2000cwr.goog legroups.com...
      : Hello,
      :
      : I have an enum:
      :
      : Public Enum Color
      : Red
      : Blue
      : Green
      : End Enum
      :
      : I need:
      :
      : 1. Loop trough all enum items and get each one value (Ex: Red)
      : and index (Ex: 1).
      :
      : For Each color As Color In [Enum].GetValues(GetT ype(Color))
      : ???
      : Next
      :
      : 2. Get an enum value (Ex: Red) by providing its index (Ex: 1)
      :
      : I have been trying to find some info about this but no success.
      :
      : Thank you for your Help,
      :
      : Miguel
      >
      The following should do what you want. Bear in mind that Enum's are
      "value" types. In the code below, the System.Enum type's .Parse and
      .GetNames functions perform "boxing" actions which are not
      particularly efficient. This code example below is deliberately
      designed to illustrate where this boxing and unboxing is occurring.
      >
      '---------------------------------------
      'VB.NET 2.0
      '---------------------------------------
      Option Strict On
      Imports System
      >
      Public Module [module]
      Public Enum Color
      Red
      Blue
      Green
      End Enum
      >
      Public Sub Main
      >
      'get a list of names for this enumeration
      For Each name As String In [Enum].GetNames(GetTy pe(Color))
      >
      'convert the enumeration name into its value equivalent.
      Dim obj As Object = [Enum].Parse(GetType( Color), name)
      Dim value As Integer = CInt(obj)
      >
      'display the name and its value
      Console.WriteLi ne(name & " = " & value)
      Next
      >
      'get a list of the possible values for the enumeration
      For Each value As Integer In [Enum].GetValues(GetT ype(Color))
      >
      'convert the value into its name equivalent
      Dim obj As Object = CObj(value)
      Dim name As String = [Enum].GetName(GetTyp e(Color), obj)
      >
      'display the value and its name
      Console.WriteLi ne(value & " = " & name)
      Next
      >
      End sub
      >
      End Module
      '---------------------------------------
      >
      Ralf
      --
      --
      ----------------------------------------------------------
      * ^~^ ^~^ *
      * _ {~ ~} {~ ~} _ *
      * /_``>*< >*<''_\ *
      * (\--_)++) (++(_--/) *
      ----------------------------------------------------------
      There are no advanced students in Aikido - there are only
      competent beginners. There are no advanced techniques -
      only the correct application of basic principles.
      Hello,

      After much reading and much googling I came up with this:
      Get names and indexes of enum:
      For Each color As Color In [Enum].GetValues(GetT ype(Color))

      Dim name As String = MyF(color, Thread.CurrentT hread.CurrentCu lture)
      Dim index As Integer = CType(color, Integer)

      Response.Write( "Name: " & name & " | Index: " & index.ToString & "<br
      />")

      Next

      MyF is a function which returns a string for a giving culture and enum
      name:

      Public Function MyF(ByVal color As Color, ByVal culture As CultureInfo)
      As String
      ....
      Get one enum name from its index:
      Dim s As String = "20"
      Dim i As Integer = CType(s, Integer)

      If Color.IsDefined (GetType(Color) , i) Then

      Dim name As Color = CType(i, Color)
      Response.Write( name.ToString)

      End If

      Need second opinion. What do you think?

      I am asking this because my Response.Write seems to show the right
      results but further on in my code I am having some problems and I am
      not sure if it comes from here and until now I wasn't able to figure
      this out.

      Thanks,

      Miguel

      Comment

      • _AnonCoward

        #4
        Re: Enum


        "shapper" <mdmoura@gmail. comwrote in message
        news:1168691664 .666816.44410@5 1g2000cwl.googl egroups.com...
        :
        : _AnonCoward wrote:
        : >
        : "shapper" <mdmoura@gmail. comwrote in message
        : news:1168635957 .974016.294280@ 11g2000cwr.goog legroups.com...
        : :
        : : Hello,
        : :
        : : I have an enum:
        : :
        : : Public Enum Color
        : : Red
        : : Blue
        : : Green
        : : End Enum
        : :
        : : I need:
        : :
        : : 1. Loop trough all enum items and get each one value (Ex: Red)
        : : and index (Ex: 1).
        : :
        : : For Each color As Color In [Enum].GetValues(GetT ype(Color))
        : : ???
        : : Next
        : :
        : : 2. Get an enum value (Ex: Red) by providing its index (Ex: 1)
        : :
        : : I have been trying to find some info about this but no success.
        : :
        : : Thank you for your Help,
        : :
        : : Miguel
        : >
        : The following should do what you want. Bear in mind that Enum's
        : are "value" types. In the code below, the System.Enum type's
        : .Parse and .GetNames functions perform "boxing" actions which
        : are not particularly efficient. This code example below is
        : deliberately designed to illustrate where this boxing and
        : unboxing is occurring.
        : >
        : '---------------------------------------
        : 'VB.NET 2.0
        : '---------------------------------------
        : Option Strict On
        : Imports System
        : >
        : Public Module [module]
        : Public Enum Color
        : Red
        : Blue
        : Green
        : End Enum
        : >
        : Public Sub Main
        : >
        : 'get a list of names for this enumeration
        : For Each name As String In [Enum].GetNames(GetTy pe(Color))
        : >
        : 'convert the enumeration name into its value equivalent.
        : Dim obj As Object = [Enum].Parse(GetType( Color), name)
        : Dim value As Integer = CInt(obj)
        : >
        : 'display the name and its value
        : Console.WriteLi ne(name & " = " & value)
        : Next
        : >
        : 'get a list of the possible values for the enumeration
        : For Each value As Integer In [Enum].GetValues(GetT ype(Color))
        : >
        : 'convert the value into its name equivalent
        : Dim obj As Object = CObj(value)
        : Dim name As String = [Enum].GetName(GetTyp e(Color), obj)
        : >
        : 'display the value and its name
        : Console.WriteLi ne(value & " = " & name)
        : Next
        : >
        : End sub
        : >
        : End Module
        : '---------------------------------------
        : >
        : Ralf
        : --
        : --
        : ----------------------------------------------------------
        : * ^~^ ^~^ *
        : * _ {~ ~} {~ ~} _ *
        : * /_``>*< >*<''_\ *
        : * (\--_)++) (++(_--/) *
        : ----------------------------------------------------------
        : There are no advanced students in Aikido - there are only
        : competent beginners. There are no advanced techniques -
        : only the correct application of basic principles.
        :
        : Hello,
        :
        : After much reading and much googling I came up with this:
        :
        : Get names and indexes of enum:
        :
        : For Each color As Color In [Enum].GetValues(GetT ype(Color))
        :
        : Dim name As String = _
        : MyF(color, Thread.CurrentT hread.CurrentCu lture)
        :
        : Dim index As Integer = CType(color, Integer)
        :
        : Response.Write( "Name: " & name & " | Index: " & _
        : index.ToString & "<br />")
        :
        : Next
        :
        : MyF is a function which returns a string for a giving culture and
        : enum name:
        :
        : Public Function MyF(ByVal color As Color, _
        : ByVal culture As CultureInfo) As String
        : ...
        :
        : Get one enum name from its index:
        :
        : Dim s As String = "20"
        : Dim i As Integer = CType(s, Integer)
        :
        : If Color.IsDefined (GetType(Color) , i) Then
        :
        : Dim name As Color = CType(i, Color)
        : Response.Write( name.ToString)
        :
        : End If
        :
        : Need second opinion. What do you think?
        :
        : I am asking this because my Response.Write seems to show the right
        : results but further on in my code I am having some problems and I am
        : not sure if it comes from here and until now I wasn't able to figure
        : this out.
        :
        : Thanks,

        What problems are you having, specifically? Also, you haven't provided
        the dteails of the MyF Function, so it's impossible to say from this
        what affect it's having later in you code.

        Ralf
        --
        --
        ----------------------------------------------------------
        * ^~^ ^~^ *
        * _ {~ ~} {~ ~} _ *
        * /_``>*< >*<''_\ *
        * (\--_)++) (++(_--/) *
        ----------------------------------------------------------
        There are no advanced students in Aikido - there are only
        competent beginners. There are no advanced techniques -
        only the correct application of basic principles.


        Comment

        Working...