Specifies that a data type or pointer data type is read only.
Specifies that the
datatype or
Ptr immediately to the right of the
Const qualifier is to be considered as read only. Read-only (
Const) declarations are a measure of type safety that can be read as 'promises not to change.' The compiler uses the const declarations to check operations on variables and parameters and generate an error at compile time if their data could potentially change. There is no runtime overhead for using
Const qualifiers since all of the checks are made at compile time.
Const can be used anywhere data type declarations are made. This includes variables, parameters, function return results, user defined type fields, type aliases, and casting. The
datatype can be any built-in standard data type or user defined type.
Read-only variables must have an initializer since modifying a read-only variable through an assignment will generate a compiler error. The initializer may appear after the declaration of the variable.
Both non-const and const variables may be passed to a procedure expecting a const parameter. However, a const variable may not be passed to a procedure taking a non-const parameter, and will generate a compile error.
Procedures can be overloaded based on the const-ness of parameters. For example a procedure can be overloaded where one version of the procedure takes a
'byref foo as bar' parameter and another version of the procedure takes a
'byref foo as const bar' parameter.
With pointer declarations,
Const can be used to indicate which part of the pointer declaration is read-only (all other parts are by default read-write). The read-only portion of the pointer data type could be the pointer itself (the address), what the pointer points to (the data), or both. In a declaration with more than one level of
Ptr indirection, the right most
Ptr indicates the highest order level of indirection and is therefore dereferenced first.
The compiler has an internal hard-limit of eight (8) levels of pointer indirection with respect to const qualifiers and the behavior of using
Const with
Ptr data types having greater than eight (8) levels of indirection is undefined.