Combining Enums

后端 未结 5 1590
旧巷少年郎
旧巷少年郎 2021-02-03 22:45

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

相关标签:
5条回答
  • 2021-02-03 23:31

    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

    0 讨论(0)
  • 2021-02-03 23:33

    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.

    0 讨论(0)
  • 2021-02-03 23:46

    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.

    0 讨论(0)
  • 2021-02-03 23:47

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

    0 讨论(0)
  • 2021-02-03 23:48

    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
    
    0 讨论(0)
提交回复
热议问题