I believe what you want is a flag type enum.
You 9 need to add the Flags attribute to the top 8 of the enum, and then you can combine enums 7 with the 'Or' keyword.
<Flags()> _ Enum CombinationEnums As Integer HasButton = 1 TitleBar = 2 [ReadOnly] = 4 ETC = 8 End Enum
Note: The numbers 6 to the right are always twice as big (powers 5 of 2) - this is needed to be able to separate 4 the individual flags that have been set.
Combine 3 the desired flags using the Or keyword:
Dim settings As CombinationEnums settings = CombinationEnums.TitleBar Or CombinationEnums.Readonly
This 2 sets TitleBar and Readonly into the enum
To 1 check what's been set:
If (settings And CombinationEnums.TitleBar) = CombinationEnums.TitleBar Then Window.TitleBar = True End If
You can use the FlagsAttribute to decorate 10 an Enum like so which will let you combine 9 the Enum:
<FlagsAttribute> _ Public Enumeration SecurityRights None = 0 Read = 1 Write = 2 Execute = 4
And then call them like so (class 8 UserPriviltes):
Public Sub New ( _ options As SecurityRights _ ) New UserPrivileges(SecurityRights.Read OR SecurityRights.Execute)
They effectively get combined 7 (bit math) so that the above user has both 6 Read AND Execute all carried around in one 5 fancy SecurityRights variable.
To check to 4 see if the user has a privilege you use 3 AND (more bitwise math) to check the users 2 enum value with the the Enum value you're 1 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
If I understand your question correctly 15 you want to combine different enum types. So 14 one variable can store a value from one 13 of two different enum's right? If you're 12 asking about storing combining two different 11 values of one enum type you can look at 10 Dave Arkell's explanation
Enums are just 9 integers with some syntactic sugar. So if 8 you make sure there's no overlap you can 7 combine them by casting to int.
It won't 6 make for pretty code though. I try to avoid 5 using enums most of the time. Usually if 4 you let enums breed in your code it's just 3 a matter of time before they give birth 2 to repeated case statements and other messy 1 antipatterns.
The key to combination
Enums is to make sure 8 that the value is a power of two (1, 2, 4, 8, etc.) so 7 that you can perform bit operations on them 6 (
Enums can be be tagged with a
Flags attribute. The 5
Anchor property on Windows Forms controls is an 4 example of such a control. If it's marked 3 as a flag, Visual Studio will let you check 2 values instead of selecting a single one 1 in a drop-down in the properties designer.
If you taking about using enum flags() there 1 is a good article here.
More Related questions