Const Statement

Declares named constants for use in place of literal values.

Syntax

Const constname [As type] = const_expression

The Const statement syntax has these parts:

Part Description
constname Required. Name of the constant; follows standard variable naming conventions.
type Optional. Data type of the variable; may be Byte, Boolean, Integer, Long, Single, Double, Date, String (for variable-length strings), String * length (for fixed-length strings), Variant. Use a separate As type clause for each variable you declare.
const_expression Required. Constatnt expression; may be numeric constant, string constant, or any combination that includes all arithmetic or logical operators.

Remarks

To combine several constant declarations on the same line, separate each constant assignment with a comma.

You can't use variables, user-defined functions, or intrinsic Basic functions (such as Chr) inexpressions assigned to constants.

    Note: Constants can make your programs self-documenting and easy to modify. Unlike variables, constants can't be inadvertently changed while your program is running.

If you don't explicitly declare the constant type using As type, the constant has the data type that is most appropriate for const_expression.

Constants declared in a Sub or Function procedure are local to that procedure. A constant declared outside a procedure is defined throughout the module in which it is declared. You can use constants anywhere you can use an expression.

Example

This example uses the Const statement to declare constants for using instead of literal values.

' Declare some constants
Const MyVar = 459
Const MyString = "HELP"

' Declare an Integer constant.
Private Const MyInt As Integer = 5

' Declare multiple constants in the  same line.
Const MyStr = "Hello", MyDouble As Double = 3.4567

 

See Also

Data Type Summary, Let Statement, Function Statement, Sub Statement