can i know how many item in ENUM )in run time) ?

D

Daylor

Public Enum COLORS

RED= 0

GREEN=1

BLUD =2

ORANGE =4

End Enum


public sub FooWithColors(enmColor as COLORS )
//here , i want to able to know this in run time :
//1.many colors i have in the enum ? (4)
//2.what is the value for each color (0,1,2,4)

end sub

how can i do that in vb.net ?

thanks and have a nice day.
 
J

Jeff Levinson [mcsd]

Use this code (it's a console app):

Module Module1

Public Enum MyColors
RED = 0
GREEN = 1
BLUD = 2
ORANGE = 4
End Enum

Sub Main()
Dim i As Integer = [Enum].GetValues(GetType
(MyColors)).Length
Console.WriteLine("Number of values = {0}",
i.ToString)

For j As Integer = 0 To i - 1
Console.WriteLine("{0} - {1}", [Enum].GetNames
(GetType(MyColors)).GetValue(j), _
Convert.ToInt32([Enum].GetValues(GetType
(MyColors)).GetValue(j)))
Next

Console.ReadLine()
End Sub

End Module

Jeff Levinson

Author of "Building Client/Server Applications with
VB.NET: An Example Driven Approach"
 

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

c# enum question 1
Enumeration: Returning the "next" item, round robin-style 2
Enum TypeConverter 3
Enum Extentions 7
How to reuse enum-definition? 3
Enum bug? 6
Enum 3
Enum string 2

Top