Constants

A constant is a meaningful name that takes the place of a value that does not change.
You should always use constants in place of hard coded values or strings.
Constants are accessed as if they were static fields.
Constants that are being accessed outside the class must be prefixed with the class name.
Constants can be marked as public, private, protected, internal or protected internal
Constants are immutable values which are known at compile time and do not change for the life of the program.
Constants must be initialized when it is declared.



Unlike variables, constants cannot be inadvertently changed at run-time.
You cannot use any variables, user defined function or any built-in functions in your declarations.



Constant Scope

The default scope for constants is Private.
Constants have scope and this scope is identical to that defined for variables.


public const int month = 12; 

Multiple constants of the same type can be declared on the same line.

const int months = 12, weeks = 52, days = 365; 


Expressions

An expression can be used that is based on other constants
Assuming there is no circular reference.



Types of Constants

The VBA Language has several types of constants.
Literal constants A specific value such as a number, date or text string that cannot change that is hard coded.
Symbolic constants - A literal constant that is represented by a name.
Enumerations - A grouping of symbolic constants into categories.



Literal Constants



Symbolic Constants



Enumerations

Enumerations are value types which default to integers.
The enum type enables you to define named constants
Please refer to the Syntax > Enumerations section for more details.



© 2018 Better Solutions Limited. All Rights Reserved. © 2018 Better Solutions Limited

Top

PrevNext