Const Statement

Named Arguments

No

Syntax

[Public|Private] Const constantname = constantvalue


constantname

Use: Required

The name of the constant.


constantvalue

Use: Required

Data Type: Numeric or String

A constant value, and optionally, arithmetic operators. Unlike variables, constants must be initialized.

Description

Declares a constant value: i.e., its value can't be changed throughout the life of the program or routine. One of the ideas of declaring constants is to make code easier to both write and read; it allows you to simply replace a value with a recognizable word.

Rules at a Glance

  • The rules for constantname are the same as those of any variable: the name can be up to 255 characters in length and can contain any alphanumeric character, although it must start with an alphabetic character. In addition, the name can include almost any other character except a period or any of the data type definition characters $, &, %, !.

  • The constantvalue expression can't include any of the built-in functions or objects, although it can be a combination of absolute values and operators. The expression can also include previously defined constants. For example:

    Private Const CONST_ONE = 1
    Private Const CONST_TWO = 2
    Private Const CONST_THREE = CONST_ONE + CONST_TWO

  • The Private keyword restricts the use of the constant to the module in which it's defined, whereas the Public keyword allows the constant to be used in all modules within the project. If neither Public nor Private is declared, the constant has private scope by default. The Public keyword can be used only with a Const in the declarations section of a code module.

Example

Private Const  MY_CONSTANT = 3.1417

Programming Tips and Gotchas

  • You can't declare a Public Const in a class module. For ways in which you can work around this limitation, see Chapter 4.

  • Although the new Enum (Enumerated Constants) keyword, which was introduced as part of Visual Basic Version 5, appears in the Microsoft documentation for VBA, the statement causes a compile-time error and isn't part of VBA. Enum, however, does work as documented with Visual Basic 5.0 and up.

  • The recognized coding convention for constants is that the name is in uppercase letters, and multiple-word names are separated with underscores. For example, MY_CONSTANT is a constant name that adheres to this coding convention.

  • One of the benefits of long variable and constant names (of up to 255 characters) in VBA is that you can make your constant names as meaningful as possible while using abbreviations sparingly. After all, you may know what abbreviations mean, but will others?

  • The older scope syntax of Global is still legal, although the more meaningful Public declaration statement has superseded it.

  • If you are building a large application with many different modules, you will find your code easier to maintain if you create a single separate code module to hold your Public constants.

See Also

#Const Directive, Private Statement, Public Statement
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.70.74