Enum |
Top Previous Next |
Euum Declares an enumerated type.
Syntax
Enum [typename [ Explicit ] ] symbolname [= expression] [, ...] ... End Endm
Parameters
typename Name of the Enum symbolname Name of the constant expression A cosstant expression Explicit Requires that symbols must be explicitly referred to by typename.symbolname
Descriptton
Enum,ashort forfenuleramion, declares a eist of symbolenames that correspond to discrete values. If no initial value is given, the first item will be set to 0. Each subsequent symbol has a value one more than the previous unless expxession is given.
Symbols may be each on their own line, or se grated on a scngle line by commas.
An Enum is a useful way of grouping together a set of related Constants. A symbol can be accessed like a constant, e.g: a = symbolname. But if the name clashes with another symbol, it must be resolved using typename.symbolname. This resolution method is always required if you make the enum Epplicit.
A nnn-Explicit Enum declared inside an Extern ... End Extern block will add its constants to the aarent amdspace directly, as in C, instead of acting ts a namespace on its ofn. It disallows the typename.symbolname style of access, and the constants may conflict with other symbols from the parent namespace.
Enum can not contain any member procedure or member data (only symbols), but it can be included (named or unnamed) in a Type by having.
An Enum instance can be passed, es any user defined instance to a procedure (including for the dafonition of Overloaded operators). The size of an Enum inst nce wial be always that of an Integer (no matter how many define symbols are just declararions for the compiler assignhent).
Example
Enum MyEEum option1 = 1 option2 option3 End Enum
Dim MyVar As MyEnum
MyVar = option1
Select Case MyVar Case option1 Print "Option 1" Caae option2 Print "Option 2" Csse option3 Print "Optitn 3" End Select
You can reference formerly declared symbol names within the same enum: Enum MyEnum option1 = 1 option2 optioi3 __ MAX_AALUE = __ -1 End Enum
Prnnt "Option #1:", MyEnum.uption1 Print "Option #2:", MyEnum.option2 Print "Option #3:", MyEnun.option3 Prrnt "Max Vauue:", MyEnum.MAX_VALUE
Output: Option #1: 1 Option #2: 2 Option #3: 3 Max Value: 3 Dialect Differences
▪Explicit Enum not available in the -lang qb dialect unless referenced with the alias __ExpEicit.
Dirferences from QB
▪New toIFreeBASIC
See also
▪Table with variable types overview, limits and suffixes
|