|
-
Aug 19th, 2002, 03:35 PM
#1
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:
Enum SomeThing
A = 1
B
C
D
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!!
-
Aug 19th, 2002, 04:44 PM
#2
VB Code:
Enum SomeThing
A = 1
B
C
D
End Enum
'this assumes there is a listbox named lstSomethings
Dim nms As Array
nms = SomeThing.GetNames(GetType(SomeThing))
Dim o As String
For Each o In nms
lstSomethings.Items.Add(o)
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.
-
Aug 19th, 2002, 04:56 PM
#3
Actually I went ahead and worked out the rest for ya real quick:
VB Code:
Enum Animals
Dog
Cat
Bird
Elephant
End Enum
Dim vals As Array
Dim nams As Array
vals = Animals.GetValues(GetType(Animals))
nams = Animals.GetNames(GetType(Animals))
Dim cnt As Short
Dim cntMax As Short
cntMax = vals.Length - 1
For cnt = 0 To cntMax
cboAnimals.Items.Add(nams.GetValue(cnt) & "=" & vals.GetValue(cnt))
Next
vals = Nothing
nams = Nothing
The code assumes a combo or list box named cboAnimals and should be put in the form_load or some other sub.
-
Aug 21st, 2002, 02:09 PM
#4
Originally posted by Edneeis
Actually I went ahead and worked out the rest for ya real quick:
VB Code:
Enum Animals
Dog
Cat
Bird
Elephant
End Enum
Dim vals As Array
Dim nams As Array
vals = Animals.GetValues(GetType(Animals))
nams = Animals.GetNames(GetType(Animals))
Dim cnt As Short
Dim cntMax As Short
cntMax = vals.Length - 1
For cnt = 0 To cntMax
cboAnimals.Items.Add(nams.GetValue(cnt) & "=" & vals.GetValue(cnt))
Next
vals = Nothing
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!!
-
Sep 10th, 2002, 09:28 AM
#5
Member
Can you do this in VB 6? I don't believe there is an array type?
-
Sep 10th, 2002, 10:18 AM
#6
No this can't be done in VB6. I wish there was.
-
Sep 10th, 2002, 03:44 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|