Results 1 to 7 of 7

Thread: Enums -> How can I get a list of all the elements in it

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Angry Enums -> How can I get a list of all the elements in it

    Meh, me = confused

    I want to get a list of all the elements in an Enum...
    let's say I have this:
    VB Code:
    1. Enum SomeThing
    2.        A = 1
    3.        B
    4.        C
    5.        D
    6. End Enum
    Let's say I want to add all the values AND all the names of the variables in that enum in a listbox...how can I do that

    so the listbox will be something like this:
    "A = 1"
    "B = 2"
    "C = 3"


    I think it's possible to do this with the parse method if you know the name of the variables... but what if I dont know that
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. Enum SomeThing
    2.                A = 1
    3.                B
    4.                C
    5.                D
    6.         End Enum
    7.  
    8.  
    9.         'this assumes there is a listbox named lstSomethings
    10.  
    11.         Dim nms As Array
    12.         nms = SomeThing.GetNames(GetType(SomeThing))
    13.         Dim o As String
    14.         For Each o In nms
    15.             lstSomethings.Items.Add(o)
    16.         Next

    That will fill in the names you'll have to mess with it bit if you want the name=value. Although there is a GetValues method just like the getNames method so it shouldn't be hard.

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Actually I went ahead and worked out the rest for ya real quick:
    VB Code:
    1. Enum Animals
    2.         Dog
    3.         Cat
    4.         Bird
    5.         Elephant
    6.     End Enum
    7.  
    8.         Dim vals As Array
    9.         Dim nams As Array
    10.         vals = Animals.GetValues(GetType(Animals))
    11.         nams = Animals.GetNames(GetType(Animals))
    12.         Dim cnt As Short
    13.         Dim cntMax As Short
    14.         cntMax = vals.Length - 1
    15.         For cnt = 0 To cntMax
    16.             cboAnimals.Items.Add(nams.GetValue(cnt) & "=" & vals.GetValue(cnt))
    17.         Next
    18.         vals = Nothing
    19.         nams = Nothing

    The code assumes a combo or list box named cboAnimals and should be put in the form_load or some other sub.

  4. #4

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by Edneeis
    Actually I went ahead and worked out the rest for ya real quick:
    VB Code:
    1. Enum Animals
    2.         Dog
    3.         Cat
    4.         Bird
    5.         Elephant
    6.     End Enum
    7.  
    8.         Dim vals As Array
    9.         Dim nams As Array
    10.         vals = Animals.GetValues(GetType(Animals))
    11.         nams = Animals.GetNames(GetType(Animals))
    12.         Dim cnt As Short
    13.         Dim cntMax As Short
    14.         cntMax = vals.Length - 1
    15.         For cnt = 0 To cntMax
    16.             cboAnimals.Items.Add(nams.GetValue(cnt) & "=" & vals.GetValue(cnt))
    17.         Next
    18.         vals = Nothing
    19.         nams = Nothing

    The code assumes a combo or list box named cboAnimals and should be put in the form_load or some other sub.
    tnx alot!!!
    Umm just one more question...I have option strict on and when I was tying to do this I got an error:
    Dim anAnimal as Animal = DirectCast(nams(0), Animal)

    that didnt work, but when I changed nams(0) to nams.GetValue(0) it worked fine. Like this:
    Dim anAnimal as Animal = DirectCast(nams.GetValue(0), Animal)


    can anyone explain why?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  5. #5
    Member
    Join Date
    Jun 2002
    Location
    NY
    Posts
    60
    Can you do this in VB 6? I don't believe there is an array type?

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    No this can't be done in VB6. I wish there was.

  7. #7

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    down with vb6
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

Posting Permissions

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



Click Here to Expand Forum to Full Width