Combining Enums

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

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

5条回答
  •  夕颜
    夕颜 (楼主)
    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:

     _
    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
    

提交回复
热议问题