469,628 Members | 1,010 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,628 developers. It's quick & easy.

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

Jan 12 '07 #1
3 5031

"shapper" <md*****@gmail.comwrote in message
news:11**********************@11g2000cwr.googlegro ups.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(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.
Jan 13 '07 #2

_AnonCoward wrote:
"shapper" <md*****@gmail.comwrote in message
news:11**********************@11g2000cwr.googlegro ups.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(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,

Miguel

Jan 13 '07 #3

"shapper" <md*****@gmail.comwrote in message
news:11*********************@51g2000cwl.googlegrou ps.com...
:
: _AnonCoward wrote:
: >
: "shapper" <md*****@gmail.comwrote in message
: news:11**********************@11g2000cwr.googlegro ups.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(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
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Jan 13 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Glenn Venzke | last post: by
21 posts views Thread by Andreas Huber | last post: by
31 posts views Thread by Michael C | last post: by
18 posts views Thread by Visual Systems AB \(Martin Arvidsson\) | last post: by
2 posts views Thread by Dennis | last post: by
1 post views Thread by Randy | last post: by
2 posts views Thread by Randy | last post: by
34 posts views Thread by Steven Nagy | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.