Vulcan.NET Code Examples

 

Vulcan.NET  Enumeration Example
/*

Enumeration sample

Syntax for enumerations is

ENUM <id> [AS <datatype>]
   MEMBER <id> [ := <constant_expr>]
   ...

All enum types have an underlying data type, which is the actual data type of all of the
enumerator values.  If not explicitly specified, the underlying data type is INT (System.Int32).   

Allowable data types are any built-in integer numeric type except System.Char.

Allowable values are integer constants or any expression that can be completely resolved at compile-time.

All enumerations are .net value classes that inherit from the abstract System.Enum type

Enumerations are types and share the same namespace as all other types, therefore an enum
cannot have the same name as a class, structure, etc.

If an enum member has no initializer, its associated value is set implicitly, according to these rules:
 - The first enum member has an associated value of zero
 - Any enum member other than the first has an associated value of the preceding member, plus one

*/

ENUM what
   MEMBER this            // associated value is 0
   MEMBER that            // associated value is 1
   MEMBER theOther        // associated value is 2

ENUM colors
   MEMBER red   := 0xff0000
   MEMBER blue  := 0x00ff00
   MEMBER green := 0x0000ff

ENUM dir
   MEMBER north           // associated value is 0
   MEMBER east  := 90
   MEMBER south := 90 * 2
   MEMBER west  := 90 * 3

ENUM foo
   MEMBER first  := 100
   MEMBER second          // associated value is 101
   MEMBER third           // associated value is 102
   MEMBER fourth := 200
   MEMBER fifth           // associated value is 201

FUNCTION DispayEnumInfo( e AS System.Enum ) AS VOID
   LOCAL enumType       AS System.Type
   LOCAL names          AS String[]
   LOCAL values         AS System.Array
   LOCAL underlyingType AS System.Type
   LOCAL count          AS INT
   LOCAL x              AS INT

   enumType       := e:GetType()
   names          := System.Enum.GetNames( enumType )
   values         := System.Enum.GetValues( enumType )
   underlyingType := System.Enum.GetUnderlyingType( enumType )
   count          := names:Length

   ?
   ? "Enumeration '" + enumType:Name + "' contains the following members with an underlying type of '" + underlyingType:ToString() + "':"

   FOR x := 1 UPTO count
      ? "   Name: '" + names[x] + "', associated value: " + Enum.Format( enumType, values:GetValue(x-1), "D" )
   NEXT

   RETURN

FUNCTION Start() AS VOID
   LOCAL e1 AS what
   LOCAL e2 AS colors
   LOCAL e3 AS dir
   LOCAL e4 AS foo

   e1 := what.this
   ? "e1 =", e1

   e2 := colors.red
   ? "e2 =", e2

   e3 := dir.south
   ? "e3 =", e3

   e4 := foo.third
   ? "e4 =", e4

   DispayEnumInfo( e1 )
   DispayEnumInfo( e2 )
   DispayEnumInfo( e3 )
   DispayEnumInfo( e4 )

   RETURN