EnumeratedTypes

 [private | public] enum identifer [as type]
    {enumvalue [ = expression]}
 end enum
  • Private – An optional keyword which ensures that an enumerated type is only available from within the module it is declared. Enumerated types are private by default.
  • Public – An optional keyword which ensures that an enumerated type is available to other programs or modules.
  • Identifier – A mandatory name, which follows the standard identifier naming conventions.
  • Type - An optional type. The type value can be byte, sbyte, short, ushort, integer or uinteger. If omitted, the type defaults to byte.
  • Firewing stores the enumerated type values as integers.
  • EnumValue – A list of unique enumerated identifiers.
  • Expression – An optional constant expression which redefines the default number assigned to each enumerated identifier.

An enumerated type is a list of unique identifiers with an optional value. When you declare a variable of type enumerated, it can only take on those values. For example,

' enumerated type...
Public Enum DeviceState
   Unknown
   Inserted
   Removed
End Enum

' set state to unknown...
Dim state As DeviceState = DeviceState.Unknown

' get the device state...
Sub GetDeviceState()
   If PORTD.1 = 0 Then
      state = DeviceState.Inserted
   Else
      state = DeviceState.Removed
   End If
End Sub

' main program...
Sub Main()
   GetDeviceState
   If state = DeviceState.Inserted Then
      Console.Write("Device is inserted", 13, 10)
   Else
      Console.Write("Device is NOT inserted", 13, 10)
   End If   
End Sub

In this example, trying to assign a value other than Unknown, Inserted or Removed to the variable State will generate a compile error. Enumerated values that don't have an explicit value expression will start at zero, with the next item being one greater than the previous item. For example,

With Console, State
   .Write(CStr(.Unknown), " = ", CStr(CByte(.Unknown)) ,13 ,10)
   .Write(CStr(.Inserted), " = ", CStr(CByte(.Inserted)) ,13 ,10)
   .Write(CStr(.Removed), " = ", CStr(CByte(.Removed)) ,13 ,10)
End With

Will output

Unknown = 0
Inserted = 1
Removed = 2

To change this default behaviour, simply assign an expression to each enumerated item. For example,

Public Enum DeviceState As Byte
   Unknown = 0
   Inserted = 100
   Removed = 200
End Enum

Note that you can give enumerated values the same value either explicitly or implicitly,

Public Enum DeviceState As Byte
   Unknown 
   Inserted = 100
   Removed = 200
   Plugged = Inserted
   Unplugged = Removed
End Enum