EnumeratedTypes

Reference.EnumeratedTypes History

Hide minor edits - Show changes to output

Changed line 53 from:
=firewing =[
to:
=firewing [=
Changed line 23 from:
Dim State As DeviceState = DeviceState.Unknown
to:
Dim state As DeviceState = DeviceState.Unknown
Changed line 28 from:
     State = DeviceState.Inserted
to:
     state = DeviceState.Inserted
Changed line 30 from:
     State = DeviceState.Removed
to:
     state = DeviceState.Removed
Changed line 37 from:
   If State = DeviceState.Inserted Then
to:
   If state = DeviceState.Inserted Then
Changed lines 1-4 from:
['''private''' | '''public'''] '''enum''' identifer ['''as''' type]
  {enumvalue [= expression]}
'''end enum'''
to:
 ['''private''' | '''public'''] '''enum''' identifer ['''as''' type]
    {enumvalue [ = expression]}
 '''end enum'''
Changed line 14 from:
=firewing =[
to:
=firewing [=
Changed line 45 from:
=firewing =[
to:
=firewing [=
Changed line 59 from:
=firewing =[
to:
=firewing [=
Changed line 67 from:
=firewing =[
to:
=firewing [=
Added lines 1-75:
['''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,
=firewing =[
' 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,
=firewing =[
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
=firewing =[
Unknown = 0
Inserted = 1
Removed = 2
=]
To change this default behaviour, simply assign an expression to each enumerated item. For example,
=firewing =[
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,
=firewing =[
Public Enum DeviceState As Byte
  Unknown
  Inserted = 100
  Removed = 200
  Plugged = Inserted
  Unplugged = Removed
End Enum
=]