Enum Statement

Declares an enumeration and specifies its members.

Syntax

Usual construct

[ @Open ] Enum [ enumeration_name ] [ Is type ]
    member_name [ = value ]
    …
End [ Enum ]

Bit flag construct

[ @Open ] @Flags Enum enumeration_name Is type
    member_name [ = value ]
    …
End [ Enum ]

Parts

@Open
Optional modifier Specifies that the enumeration can be extended.
@Flags
Changes the enumeration into a set of bit flags.
enumeration_name
A name for the enumeration
Optional in the usual construct
Mandatory with @Open and the bit-flag construct
type
An integer data type or a different enumeration. It sets the range of possible values and the minimum value.
Optional in the usual construct
Mandatory in the bit-flag construct
member_name
Mandatory name
value
Optional constant integer expression
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 integer 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 “=”, then its default value is zero. Members of an enumeration can be positive or negative. Members can have the same value, but the names in an enumeration must be different.

If type is a different enumeration, then 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, Int, UInt, 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, then it causes a compiler error. Members can have the same values, but the names in a set of flags must be different.

If type is the name of a bit field, then 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 four areas where you can change the syntax of the statement:

See @Option Directive for more information.

Examples

Listing 1

@Open Enum fruit
    apple
    banana
    cherry
End Enum

Listing 2: Extending an enumeration

Enum more_fruit Is fruit
    peach
    pear
    pineapple
End Enum

Listing 3: A set of bit flags

@Flags Enum days Is Int8
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
End Enum

Var weekend As days = Saturday Or Sunday

See also