Emum to String

  • Thread starter Thread starter Stuart Shay
  • Start date Start date
S

Stuart Shay

Hello All:

I have the following Emuneration which I am using in my Web Application

Enum ROLE As Integer

APPLICATION_VIEWER = 1
USER_GROUP_ADMIN = 2
SECURITY = 4
NODE_ADMIN = 8
ADMIN = 16
POWER_USER = 32
USER = 64

End Enum

If I have BitMask how do I return the Value so I can use it as a String
which looks like this

65 = APPLICATION_VIEWER|USER
41 = APPLICATION_VIEWER|NODE_ADMIN|POWER_USER

Thanks
Stuart
 
Hi,

Apply the FlagsAttribute on the enumeration. Then calling ToString() on the
ROLE type variable will return you the comma seaprated list of individual
bit masks. For example:

<Flags> _
Enum ROLE As Integer
APPLICATION_VIEWER = 1
USER_GROUP_ADMIN = 2
SECURITY = 4
NODE_ADMIN = 8
ADMIN = 16
POWER_USER = 32
USER = 64
End Enum

Dim a As ROLE
a = ROLE.APPLICATION_VIEWER Or ROLE.USER_GROUP_ADMIN

Now, a.ToString() will return "APPLICATION_VIEWER, USER_GROUP_ADMIN". You
can then replace all ', ' occurances with '|'.

Hope, I got your question correctly.


Hello All:

I have the following Emuneration which I am using in my Web Application

Enum ROLE As Integer

APPLICATION_VIEWER = 1
USER_GROUP_ADMIN = 2
SECURITY = 4
NODE_ADMIN = 8
ADMIN = 16
POWER_USER = 32
USER = 64

End Enum

If I have BitMask how do I return the Value so I can use it as a String
which looks like this

65 = APPLICATION_VIEWER|USER
41 = APPLICATION_VIEWER|NODE_ADMIN|POWER_USER

Thanks
Stuart
 
Shiva

Thanks, But I need the reverse, I have a method that returns 41 and I need
to Convert this to the String APPLICATION_VIEWER|NODE_ADMIN|POWER_USER

Example

Input: 41
Output: APPLICATION_VIEWER|NODE_ADMIN|POWER_USER

Input 65
Output: APPLICATION_VIEWER|USER

<Flags> _
Enum ROLE As Integer
APPLICATION_VIEWER = 1
USER_GROUP_ADMIN = 2
SECURITY = 4
NODE_ADMIN = 8
ADMIN = 16
POWER_USER = 32
USER = 64
End Enum

Thanks
Stuart
 
Ok, you can declare a variable of type ROLE and assign the numeric value to
it directly. Eg:

Dim a As Role
Dim s As String
a = 41
s = a.ToString().Replace (", ", "|")
Console.WriteLine (s) 'Prints APPLICATION_VIEWER|NODE_ADMIN|POWER_USER

Hope I got it correctly now :-)

Shiva

Thanks, But I need the reverse, I have a method that returns 41 and I need
to Convert this to the String APPLICATION_VIEWER|NODE_ADMIN|POWER_USER

Example

Input: 41
Output: APPLICATION_VIEWER|NODE_ADMIN|POWER_USER

Input 65
Output: APPLICATION_VIEWER|USER

<Flags> _
Enum ROLE As Integer
APPLICATION_VIEWER = 1
USER_GROUP_ADMIN = 2
SECURITY = 4
NODE_ADMIN = 8
ADMIN = 16
POWER_USER = 32
USER = 64
End Enum

Thanks
Stuart
 
Back
Top