This documentation is archived and is not being maintained.

Constants Overview

Code often contains constant values that reappear. It can also depend on certain numbers that are difficult to remember or have no obvious meaning.

In these cases, you can greatly improve the readability of your code and make it easier to maintain by using constants. A constant is a meaningful name that takes the place of a number or string that does not change. Constants store values that, as the name implies, remain constant throughout the execution of an application.

You declare a constant with the Const statement, using the same guidelines you would for creating a variable name. If Option Strict is On, you must explicitly declare the constant type.

A constant's scope is the same as that of a variable declared in the same location. To create a constant that exists within the scope of a particular procedure, declare it inside that procedure. To create a constant that is available throughout an application, declare it using the Public keyword in the declarations section of the class.

Note   Although constants somewhat resemble variables, you cannot modify them or assign new values to them as you can to variables.

The constants you use in your code can be intrinsic to the object model for controls or components you work with, or they can be user-defined (that is, those you create yourself).

See Also

User-Defined Constants | Declaring Constants | Constant and Literal Data Types | Enumerations Overview | Declaring Enumerations | Enumerations and Name Qualification | Intrinsic Constants and Enumerations | Choice of Variable Scope