How to make a list of values and names of an enumaration
In userinterface you need to show a list the names off an enumaration but you want to store the value
I've made a class that just to provide for these cases:
Code:
Public Class SelectionListData
Implements Iclonable
Private _Id As Integer
Private _Description As String
Public Sub New()
_Id = 0
_Description = String.empty
End Sub
Public Sub New(ByVal Id As Integer, ByVal Description As String)
Me.New()
_Id = Id
_Description = Description
End Sub
Public Property Id() As Integer
Get
Return _Id
End Get
Set(ByVal value As System.Nullable(Of Integer))
_Id = value
End Set
End Property
Public Property Description() As String
Get
Return _Description
End Get
Set(ByVal value As String)
if value is nothing then
value = String.Empty
end if
_Description = value
End Set
End Property
Public Function Clone() As Object Implements System.ICloneable.Clone
Dim Cloneobject As SelectionListData
Cloneobject = Me.memberwiseclone
Return Cloneobject
End Function
Public Shared Function MakeSelectionListFromEnum(ByVal EnumToList As Type) As List(Of SelectionListData)
Dim Values As Array
Dim Names As Array
Dim Itm As SelectionListData
MakeSelectionListFromEnum = New List(Of SelectionListData)
Values = System.Enum.GetValues(EnumToList)
For Sh As Short = 0 To Values.Length - 1
Itm = New SelectionListData
Itm.Id = Values.GetValue(Sh)
Itm.Description = Values(Sh).ToString
MakeSelectionListFromEnum.Add(Itm)
Next
End Function
End Class
To use it in code :
Dim X as List(of SelectionListData)
X = SelectionListData.MakeSelectionListFromEnum(GetType(YourEnum))