Show/Hide Toolbars

XSharp

Purpose

Declare an enum to  the compiler.

Syntax

 [attributes] [Modifiers]

 ENUM <idEnumName> [AS type]  

         memberName [:= value]

           [...]

 END [ENUM]

 

Arguments

AttributesAn optional list of one or more attributes that describe meta information for am entity, such as for example the [TestMethod] attribute on a method/function containing tests in a MsTest class library. Please note that Attributes must be on the same line or suffixed with a semi colon when they are written on the line above that keyword.

 

ModifiersAn optional list of modifiers that specify the visibility or scope of the entity, such as PUBLIC, PROTECTED, HIDDEN, INTERNAL, SEALED, ABSTRACT or STATIC.

 

idEnumA valid identifier name for the enum.  Enum names must be unique within a namespace.

 

AS type The data type of the enumeration members (optional).

 

memberName The name(s) of the enumeration members.

 

Description

A type declared with the ENUM keyword is an "enumeration" type, a type that consists of a set of named constants which are called the enumerator list. Enumeration types implicitly inherit from System.Enum.

 

An enumeration type has an underlying type, which is the type of the items in the enumerator list. The default underlying type is INT; if the AS clause is specified then the type can be any signed or unsigned integral type except System.Char.

 

By default, the first member in the enumerator list has a value of 0.  The value of every other member is the value of the previous member plus 1. An element's value can be explicitly set to any value in the range of the underlying type by using the assignment operator (:=) followed by either a literal integer value, or an expression that resolves to an integer value at compile time.  Two or more members can explicitly set to the same value.

 

The default value for an ENUM E is the expression (E)0., which may or may not represent a member of the enum type, depending on whether or not a member having the value of 0 exists.

 

Enumeration members are referenced by specifying the name of the enumeration type and the name of the member, separated by a period. If the enumeration type was declared in the namespace currently being compiled, the name of the enumeration type is sufficient. Otherwise, either the fully-qualified name of the enumeration must be used (e.g. System.Windows.Forms.MessageBoxButtons) or the namespace must be imported using the USING directive.

 

The System.FlagsAttribute attribute may be placed on an ENUM to indicate that the elements of the enum may be combined using a bitwise OR operation.

 

The enum name may include one or more namespace names, separated by periods. If no period is found in the enum name, then the default namespace is assumed. The default namespace is the base name of the output assembly name, unless explicitly overridden by the /ns compiler option.

 

Example

ENUM days
  Sunday     // 0
  Monday     // 1
  Tuesday   // 2
  Wednesday // 3
  Thursday   // 4
  Friday     // 5
  Saturday   // 6
END CLASS
 
FUNCTION example1() AS VOID
  LOCAL i AS INT
  i := (INT) days.Friday
  ? days.Friday, i     // prints: Friday 5
 
ENUM direction
  north       // has default value of 0
  east  := 90
  south := 180
  west  := 270
END ENUM
 
FUNCTION example2( x AS direction ) AS VOID
  IF x == direction.north
     goNorth()
  ELSEIF x == direction.east
     goEast()
  ELSEIF x == direction.south
     goSouth()
  ELSEIF x == direction.west
     goWest()
  ELSE
     Debug.Assert( "Unknown value for direction" )
  ENDIF
 
[System.FlagsAttribute];
ENUM CarOptions
  SunRoof       := 0x1
  Spoiler       := 0x2
  FogLights     := 0x4
  TintedWindows := 0x8
END ENUM
 
FUNCTION example3() AS VOID
  LOCAL options AS CarOptions
  options := CarOptions.SunRoof | CarOptions.FogLights
  ? options       // prints: SunRoof, FogLights
  ? (INT) options // prints: 5

In the first and third examples, note that using the ? statement on an enum type prints out the textual value of the enum. This is because the names of the enum members are stored in the assembly's metadata, and the ToString() method in System.Enum (which every enum type inherits from) uses the metadata to obtain and return the name of the enum member, rather than its underlying numerical value.

 

Also note that even though the textual names of the enum members are returned from Enum.ToString(), the compiler uses the literal numeric values in the compiled code. So the expression IF x == direction.north for example actually compiles as IF x == 0, because the mapping between enumeration members and their underlying values occurs at compile time, not at runtime.  This makes using enumeration types as efficient as #define, while providing much higher level of type safety and compile time error checking. However, this also means that changing the values of enum members can cause existing code that uses the enum to break unless it is recompiled.

 

Notes