Enumeration

S

shapper

Hello,

I have an Enum:

Public Enum City
NewYork
London
End Enum

How do I get the index of a certain item?

Thanks,
Miguel
 
J

Jacob

All enum values should equal an integer value, so you can just cast it
as an integer.
 
S

shapper

Jacob said:
All enum values should equal an integer value, so you can just cast it
as an integer.

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
 

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

Similar Threads

Trouble calling a function with enum parameter 3
DropDownList Issue 0
String to Enumeration 1
Create Enum at runtime 1
Enumeration 0
Find String 3
Enum 4
Generic List to Array 2

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
TopCryptoTxSoftwares2024

Latest Threads

Top