Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

VB.Net: Combine enums

From Wiki

Jump to: navigation, search

You can combine enums like VB does itself in more then one instance. For this to work you will have to choose powers of 2 as the value of the enum and use the Flags attribute. Something like this

  1. Module Module1
  2.  
  3.     Sub Main()
  4.         CheckColor(Redcolors.Pink)
  5.         CheckColor(Redcolors.Red)
  6.         CheckColor(Bluecolors.Blue)
  7.         CheckColor(Bluecolors.DarkBlue)
  8.         CheckColor(Bluecolors.Blue Or Redcolors.Red)
  9.         CheckColor(Purplecolors.Purple)
  10.         Console.ReadLine()
  11.     End Sub
  12.  
  13.     Private Sub CheckColor(ByVal color As Integer)
  14.         If Not color = Purplecolors.Purple Then
  15.             If Not color = Bluecolors.Blue Then
  16.                 Console.WriteLine("this is not blue or purple")
  17.             Else
  18.                 Console.WriteLine("this is blue")
  19.             End If
  20.         Else
  21.             Console.WriteLine("this is purple")
  22.         End If
  23.     End Sub
  24.  
  25.  
  26.     <Flags()> _
  27.     Public Enum Redcolors As Integer
  28.         Red = 1
  29.         Pink = 2
  30.     End Enum
  31.  
  32.     <Flags()> _
  33.     Public Enum Bluecolors As Integer
  34.         Blue = 4
  35.         DarkBlue = 8
  36.     End Enum
  37.  
  38.     <Flags()> _
  39.     Public Enum Purplecolors As Integer
  40.         Purple = 5
  41.     End Enum
  42. End Module

593 Rating: 0.0/5 (0 votes cast)