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

  • Thread starter Thread starter Daylor
  • Start date Start date
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.
 
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"
 
Back
Top