Advertisement
Advertisement


Combining Enums


Question

Is there a way to combine Enums in VB.net?

2008/09/12
1
27
9/12/2008 9:14:08 AM

Accepted Answer

I believe what you want is a flag type enum.

You need to add the Flags attribute to the top of the enum, and then you can combine enums with the 'Or' keyword.

Like this:

<Flags()> _
Enum CombinationEnums As Integer
  HasButton = 1
  TitleBar = 2
  ReadOnly = 4
  ETC = 8
End Enum

Note: The numbers to the right are always twice as big (powers of 2) - this is needed to be able to separate the individual flags that have been set.

Combine the desired flags using the Or keyword:

Dim settings As CombinationEnums
settings = CombinationEnums.TitleBar Or CombinationEnums.Readonly

This sets TitleBar and Readonly into the enum

To check what's been set:

If (settings And CombinationEnums.TitleBar) = CombinationEnums.TitleBar Then
  Window.TitleBar = True
End If
2008/09/12
46
9/12/2008 11:57:14 AM

You can use the FlagsAttribute to decorate an Enum like so which will let you combine the Enum:

<FlagsAttribute> _
Public Enumeration SecurityRights
None = 0
Read = 1
Write = 2
Execute = 4

And then call them like so (class UserPriviltes):

Public Sub New ( _
    options As SecurityRights _
)

New UserPrivileges(SecurityRights.Read OR SecurityRights.Execute)

They effectively get combined (bit math) so that the above user has both Read AND Execute all carried around in one fancy SecurityRights variable.

To check to see if the user has a privilege you use AND (more bitwise math) to check the users enum value with the the Enum value you're checking for:

//Check to see if user has Write rights
If (user.Privileges And SecurityRights.Write = SecurityRigths.Write) Then
    //Do something clever...
Else
    //Tell user he can't write.
End If

HTH, Tyler

2014/03/19

If you taking about using enum flags() there is a good article here.

2008/09/12

If I understand your question correctly you want to combine different enum types. So one variable can store a value from one of two different enum's right? If you're asking about storing combining two different values of one enum type you can look at Dave Arkell's explanation

Enums are just integers with some syntactic sugar. So if you make sure there's no overlap you can combine them by casting to int.

It won't make for pretty code though. I try to avoid using enums most of the time. Usually if you let enums breed in your code it's just a matter of time before they give birth to repeated case statements and other messy antipatterns.

2008/09/12

The key to combination Enums is to make sure that the value is a power of two (1, 2, 4, 8, etc.) so that you can perform bit operations on them (|= &=). Those Enums can be be tagged with a Flags attribute. The Anchor property on Windows Forms controls is an example of such a control. If it's marked as a flag, Visual Studio will let you check values instead of selecting a single one in a drop-down in the properties designer.

2015/09/22

Source: https://stackoverflow.com/questions/58517
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]