SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist perpetual_dream's Avatar
    Join Date
    Nov 2005
    Location
    Jerusalem
    Posts
    542
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fill dropDown list with Enum?

    Hello,

    I have found an example online that would fill a dropdownlist with an enum

    Code VBNET:
    Private Enum UserTypeEnum
       Administrator = 0
       Gold_Customer = 1
       Silver_Customer = 2
       Bronze_Customer = 3
       Visitor = 4
    End Enum
     
     
     
    Private Sub FillUserType()
     ' Loop through the entries in numerical order
     For Each iValue As Int32 In _
         System.Enum.GetValues(GetType(UserTypeEnum))
      Dim li As New ListItem
      li.Value = iValue.ToString
      li.Text =[Enum].GetName(GetType(UserTypeEnum), _
                  iValue).Replace("_", " ")
      li.Selected = False
      cboUserType.Items.Add(li)
      li = Nothing
     Next
    End Sub

    I tried to create a function for it by sending the enum, dropdownlist and returning the filled dropdownlist.

    Code VBNET:
        Public Function FillUserType(ByVal EnumCollectionx As System.Enum, ByVal Cbo As DropDownList) As DropDownList
            ' Loop through the entries in numerical order
            For Each iValue As Int32 In _
                [COLOR="Red"]System.Enum.GetValues(GetType(EnumCollectionx))[/COLOR]
                Dim li As New ListItem
                li.Value = iValue.ToString
                li.Text = [Enum].GetName(GetType(EnumCollectionx), _
                            iValue).Replace("_", " ")
                li.Selected = False
                Cbo.Items.Add(li)
                li = Nothing
            Next
        End Function

    I keep getting an error at System.Enum.GetValues(GetType(EnumCollectionx))[/: Type 'EnumCollectionx' is not defined.

    How can I create such a Function?
    Tanzeelnet - Best arabic downloadsite
    Linux Hosting |Free Downloads |

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Location
    USA
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well in C# I usually use Enum.GetNames(typeof(Enum)) which returns a string array.

    Example:

    Code Csharp:
    string[] names = Enum.GetNames(typeof(NameOfEnum));
                for (int i = 0; i < names.Length; i++)
                {
                    names[i] = names[i].Replace("_", " ");    
                }

    then to get the value just cast it. (NameOfEnum)value

    Does VB.NET have that method?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •