Enumeration. Get values

L

Laurent Bugnion [MVP]

Hi,
Hello,

Is there a way to loop trough all enumeration values?

Thanks,

Miguel

Yes:

enum MyEnum
{
// ...
}

Then:

MyEnum[] allEnumValues = (MyEnum[]) Enum.GetValues( typeof( MyEnum ) );

Then you can loop using foreach.

HTH,
Laurent
 
S

shapper

Hi,

I used the following code:

Public Enum Roles

End Enum

' Loop through each roles enumeration name
For Each role As String In Roles.GetNames(GetType(Roles))
MyFunction(role)
Next

This is working if MyFunction is as follows:
Function MyFunction(ByVal role As String)

However, I would like to use something like:
Function MyFunction(ByVal role As Roles)

How can I make this work?

Thanks,
Miguel

Hi,
Hello,

Is there a way to loop trough all enumeration values?

Thanks,

Miguel

Yes:

enum MyEnum
{
// ...
}

Then:

MyEnum[] allEnumValues = (MyEnum[]) Enum.GetValues( typeof( MyEnum ) );

Then you can loop using foreach.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
L

Laurent Bugnion [MVP]

Hi,
Hi,

I used the following code:

Public Enum Roles

End Enum

' Loop through each roles enumeration name
For Each role As String In Roles.GetNames(GetType(Roles))
MyFunction(role)
Next

This is working if MyFunction is as follows:
Function MyFunction(ByVal role As String)

However, I would like to use something like:
Function MyFunction(ByVal role As Roles)

How can I make this work?

Thanks,
Miguel

Using GetNames returns the names as string. Using GetValues returns the
values as enumeration, so this is what you want.

I described it to you in my prior post quoted below.

HTH,
Laurent

enum MyEnum
{
// ...
}

Then:

MyEnum[] allEnumValues = (MyEnum[]) Enum.GetValues( typeof( MyEnum ) );

Then you can loop using foreach.
 
O

Olaf Rabbachin

Hi,
I used the following code:

Public Enum Roles

End Enum

' Loop through each roles enumeration name
For Each role As String In Roles.GetNames(GetType(Roles))
MyFunction(role)
Next

This is working if MyFunction is as follows:
Function MyFunction(ByVal role As String)

However, I would like to use something like:
Function MyFunction(ByVal role As Roles)

to me it seems that you simply misunderstood something.
If you would like to have an argument in your function that is geared at an
enum, you should actually be fine with the way you did above. All you need
in order to use the enum in your function is a) from the caller, you need
to call it as in ClassName.EnumName.EnumValue and b) the function utilizing
the value must be within the enum's scope or the enum must also include the
complete path ([namespace], class-name, etc.).
Consider this:

public class MyClass
public enum MyEnum
MyValue1=1
MyValue2=2
end enum

public sub MyFunction(byval x as MyEnum) as boolean
select case x
case MyEnum.MyValue1
'...
case MyEnum.MyValue2
'...
case else
'...
end select
return ...
end sub
end class

A call to the above function from outside of MyClass could look like this:
dim cM as MyClass
dim XY as boolean=cm.MyFunction(MyClass.MyEnum.MyValue2)

Hence, is it possible that you took it the wrong way and there isn't any
need for GetNames ..? At least I don't see what you would need it for in
your scenario.

Cheers,
Olaf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top