Constants and Enumerations

Constants

Constants let you give applicable names to values that do not change. They can make code clearer and easier to keep correct. They are an alternative to literal numbers and strings that get used randomly through all the code and can cause errors.

A constant cannot be changed after its value is set. This gives code that uses them as an alternative to variables increased safety. Safety becomes more important when many persons write code for the same program.

You can make constants with the statement Const. Such constants have the same scope as variables declared in the same code block. For example, if a constant is declared in a procedure, then it is visible only in that one procedure. If you put Const at module scope, then it makes the constant visible to the full module, but not to other modules.

Enumerations

Enumerations make it easy to use sets of related constants. A frequently used example is the days of the week. You assign a different number to each day, then use the constants as alternatives to the numbers in your code.

You can make enumerations with the statement Enum. You can put them in module scope or in blocks such as Class or Object.

Enumerations are globally visible through all of an application. But different enumerations can have constants with the same name. When this occurs, the constant must have its name qualified.

The statement Enum can give an enumeration a type name, or it can be without a name. Enumeration types can be used after As in variable and parameter declarations. Such variables can be assigned the value of an enumeration constant without qualification.

See also