Enum
Statement
Declares an enumeration and specifies its members.
Syntax
Usual construct
[ modifiers ] _
Enum enumeration_name [ Is base_type ]
member_name [ = value ]
…
End [ Enum ]
Bit flag construct
[ modifiers ] _
@Flags Enum enumeration_name Is base_type
member_name [ = value ]
…
End [ Enum ]
Parts
modifiers
Optional modifiers:
@Deprecated
– See @Deprecated.@Flags
– Changes the enumeration into a set of bit flags.@MustUse
– Code that calls a procedure that returns the enumeration type must use the result. See @MustUse.@Open
– The enumeration can be extended.@Strict
– Variables of the enumeration type can hold only the given constants.@Unique
– All the values must be different.
enumeration_name
- Mandatory name for the enumeration
Is
- A keyword before
base_type
. - You can also use one of the non-reserved keywords
Extends
orInherits
as an alternative. base_type
- A numeric data type,
String
, or a different enumeration. It sets the range of possible values and the minimum value. - Optional in the usual construct.
- Mandatory integer data type with the modifier
@Flags
. member_name
- Mandatory name
- You can put one or more members on one line.
Use a semicolon (
;
) between each member on the same line. A semicolon is also permitted at the end of a line. value
- Optional constant expression.
It must have the same type as
base_type
, or automatically convert to that type. - Not permitted without
base_type
. - Mandatory with the directive
@Option Enum Equals
. End
- Completes the statement.
You can also use
End Enum
. - You can change the syntax of this part. See @Option Directive for more information.
Instructions
An enumeration is a set of related constants. They can make code easier to read and change. They are an alternative to literals found in random locations in code.
Usually it is not necessary to directly set a constant's value. The compiler can make sure that each one is different.
Usual construct
If an enumeration is not an extension to a different one and the first member is not given a value with the operator “=
”, its default value is zero.
Members of an enumeration can be positive or negative.
members can have the same value, unless given the modifier @Unique
, but the names in an enumeration must be different.
If type
is a different enumeration, the new one is an extension of type
.
Thus the default value of the first member is the largest member of type
plus one.
Bit-flag construct
The bit-flag construct looks almost the same as the usual construct, but has important differences.
Usual enumerations can only be assigned and compared.
But flags can be used with the logical operators: And
, Or
, Xor
, and Not
.
Each uninitialized member is given the value that agrees with the “least significant bit” not used at that point. The first member thus has a default value of one, the second is 2, the third is 4, the fourth is 8, etc.
type_name
is usually one of the primitive integer types:
Int8
, UInt8
, Int16
, UInt16
, Int32
, UInt32
, Int64
, UInt64
, Int128
, or UInt128
.
A member can be given one value in the range of type
.
If you try to initialize a member with a value out of range, it causes a compiler error.
Members can have the same values, unless given the modifier @Unique
, but the names in a set of flags must be different.
If type
is the name of a bit field, the new statement is an extension to the flags.
The range of an extension is the same as its parent.
Changes in syntax with @Option
There are three areas where you can change the syntax of the statement:
@Option Type Extends
,@Option Type Inherits
,@Option Type Is
@Option Enum Equals
@Option End Block
and@Option End
See @Option Directive for more information.
Examples
Example 1
@Open Enum fruit
apple
banana
cherry
End Enum
The equivalent code with values written:
@Open Enum fruit
apple = 0
banana = 1
cherry = 2
End Enum
Example 2: Extending an enumeration
Enum more_fruit Is fruit
peach; pear; pineapple
End Enum
The enumeration more_fruit
has these members:
(0) apple
, (1) banana
, (2) cherry
, (3) peach
, (4) pear
, and (5) pineapple
.
Example 3: A set of bit flags
@Flags Enum days Is Int8
Monday ' = 1 (00000001)
Tuesday ' = 2 (00000010)
Wednesday ' = 4 (00000100)
Thursday ' = 8 (00001000)
Friday ' = 16 (00010000)
Saturday ' = 32 (00100000)
Sunday ' = 64 (01000000)
End Enum
Var weekend As days = Saturday Or Sunday