Last post May 18, 2009 01:40 PM by Tuppers
May 18, 2009 07:58 AM|Tuppers|LINK
Hi I am having a problem thinking of how to store some information in my design and I am looking for some help:
I currently have an enum decalred like this:
Public Class Enums
Public Enum Divs
ADIV = 1
BDIV = 2
CDIV = 3
DDIV = 4
EDIV = 5
FDIV = 6
GDIV = 7
I then use the following code in my Helpers file:
Public Shared Function GetEnumForBind(ByVal enumeration As Type) As Hashtable
Dim names As String() = [Enum].GetNames(enumeration)
Dim values As Array = [Enum].GetValues(enumeration)
Dim ht As New Hashtable()
For i As Integer = 0 To names.Length - 1
I then have this on my page:
Dim ht2 As Hashtable = Helpers.GetEnumForBind(GetType(Enums.Divs))
DropDownList2.DataSource = ht2
DropDownList2.DataTextField = "value"
DropDownList2.DataValueField = "key"
Works great and creates the list but the list is not in the order I put it in the enum it starts off at DDIV!! Also I am thinking I would like to put something like A Division = 1 or A and so on! So I am thinking that I may have to steer away from this method
and move to an array instead? Can anyone suggest a method of storing this?
May 18, 2009 09:22 AM|MetalAsp.Net|LINK
How about returning a Dictionary<int,
string> instead. The int will be your key (the enum values) and the string will be whatever text you want to associate with the key.
May 18, 2009 01:40 PM|Tuppers|LINK
Not sure how i would use that sorry but thanks for the reply? Could you please ellaborate?
Also whilst reading and many many searches I came up with something that kinda goes like this.
Public Enum Divs
<Description("-- Select --")> NotSelected = 0
<Description("A - Division")> ADIV = 1
<Description("B - Division")> BDIV = 2
<Description("C - Division")> CDIV = 3
<Description("D - Division")> DDIV = 4
<Description("E - Division")> EDIV = 5
<Description("F - Division")> FDIV = 6
GDIV = 7
I have the following code to get the description:
Public Shared Function GetEnumDescription(ByVal value As [Enum]) As String
Dim fi As FieldInfo = value.[GetType]().GetField(value.ToString())
Dim attributes As DescriptionAttribute() = DirectCast(fi.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
Return (If((attributes.Length > 0), attributes(0).Description, value.ToString()))
I am just not sure how to use it in the Sub GetEnumForBind i.e. overiding the name value I am currenlty populating the hash table with?? Can any one make a suggestion?