Enumeration iteration

  • Thread starter Thread starter Matthew
  • Start date Start date
M

Matthew

Hi

This has been irritating me for a while, so I think its not possible,
but hopefully Im wrong...

Is it possible to iterate an enum ? I want to do something like this.

for each enum in enumname
'do something (eg add item to select box)
next

thanks,

Matthew
 
Matthew said:
This has been irritating me for a while, so I think its not
possible, but hopefully Im wrong...

Is it possible to iterate an enum ? I want to do something like
this.

for each enum in enumname
'do something (eg add item to select box)
next


Dim Name As String
Dim Names As String()

Names = [Enum].GetNames(GetType(FormBorderStyle))

For Each Name In Names
Next
 
* (e-mail address removed) (Matthew) scripsit:
This has been irritating me for a while, so I think its not possible,
but hopefully Im wrong...

Is it possible to iterate an enum ? I want to do something like this.

for each enum in enumname
'do something (eg add item to select box)
next

'Enum.GetNames', 'Enum.GetValues'.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>
 
Matthew,
If you want values instead of names (Armin showed you names) try:

Dim value As FormBorderStyle
Dim values As FormBorderStyle()

values = DirectCast([Enum].GetValues(GetType(FormBorderStyle)),
FormBorderStyle())

For Each value In values
Next

Also, System.Enum (the base type for all enums) have a number of other
useful shared functions, such as Format, Parse, IsDefined, and ToObject.
Remember because Enum is a keyword, you need to escape [Enum] in square
brackets to use the System.Enum type.

Hope this helps
Jay
 
Jay B. Harlow said:
Matthew,
If you want values instead of names (Armin showed you names) try:

Dim value As FormBorderStyle
Dim values As FormBorderStyle()

values = DirectCast([Enum].GetValues(GetType(FormBorderStyle)),
FormBorderStyle())

For Each value In values
Next

Also, System.Enum (the base type for all enums) have a number of other
useful shared functions, such as Format, Parse, IsDefined, and ToObject.
Remember because Enum is a keyword, you need to escape [Enum] in square
brackets to use the System.Enum type.

Hope this helps
Jay

Matthew said:
Hi

This has been irritating me for a while, so I think its not possible,
but hopefully Im wrong...

Is it possible to iterate an enum ? I want to do something like this.

for each enum in enumname
'do something (eg add item to select box)
next

thanks,

Matthew


Thanks very much Jay and Armin.
 
Jay B. Harlow said:
Matthew,
If you want values instead of names (Armin showed you names) try:

Dim value As FormBorderStyle
Dim values As FormBorderStyle()

values = DirectCast([Enum].GetValues(GetType(FormBorderStyle)),
FormBorderStyle())

For Each value In values
Next

Also, System.Enum (the base type for all enums) have a number of other
useful shared functions, such as Format, Parse, IsDefined, and ToObject.
Remember because Enum is a keyword, you need to escape [Enum] in square
brackets to use the System.Enum type.

Hope this helps
Jay

Matthew said:
Hi

This has been irritating me for a while, so I think its not possible,
but hopefully Im wrong...

Is it possible to iterate an enum ? I want to do something like this.

for each enum in enumname
'do something (eg add item to select box)
next

thanks,

Matthew


For anyone interested here is how I have implemented the population of
a selectbox from an enum, using the advice of Jay and Armin: (XKeyType
is the Enum)

Dim val As XKeyType
Dim enumvals As XKeyType() =
DirectCast([Enum].GetValues(GetType(XKeyType)), XKeyType())
Dim li As ListItem
For Each val In enumvals
li = New ListItem
With li
.Value = val
.Text = [Enum].Format(GetType(XKeyType), val, "g")
End With
ddlist1.Items.Add(li)
Next
 
Back
Top