Enum

S

shapper

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(GetType(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
 
A

_AnonCoward

: 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(GetType(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(GetType(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.WriteLine(name & " = " & value)
Next

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

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

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

End sub

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

Ralf
 
S

shapper

_AnonCoward said:
: 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(GetType(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(GetType(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.WriteLine(name & " = " & value)
Next

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

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

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

End sub

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

Ralf
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *

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(GetType(Color))

Dim name As String = MyF(color, Thread.CurrentThread.CurrentCulture)
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
 
A

_AnonCoward

:
: _AnonCoward wrote:
: >
: > : > :
: > : 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(GetType(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(GetType(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.WriteLine(name & " = " & value)
: > Next
: >
: > 'get a list of the possible values for the enumeration
: > For Each value As Integer In [Enum].GetValues(GetType(Color))
: >
: > 'convert the value into its name equivalent
: > Dim obj As Object = CObj(value)
: > Dim name As String = [Enum].GetName(GetType(Color), obj)
: >
: > 'display the value and its name
: > Console.WriteLine(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(GetType(Color))
:
: Dim name As String = _
: MyF(color, Thread.CurrentThread.CurrentCulture)
:
: 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top