tags:

views:

1360

answers:

5
+6  Q: 

Combining Enums

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

A: 

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

Hath
+1  A: 

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.

Mendelt
+1  A: 

The key to combine enums is to make sure that the value is a power of two (0, 2, 4, 8) 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 in stead of select a single one in a drop down in the properties designer.

Jonas Follesø
Powers of two: don't forget 1 = 2^0!
Konrad Rudolph
+8  A: 

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
Dave Arkell
Great answer. In case it isn't immediately obvious, (as Jonas Follesø points out in his answer) - the key to this is using Enum values to the power of 2 (1,2,4,8,16,etc)
Andrew
+2  A: 

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 = 3
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

Tyler
btw, it's not recommended to have flag meaning 'no flag selected'. http://msdn.microsoft.com/en-us/library/ms229062.aspx
abatishchev
why not use a BitVector32?
Maslow