Constants Overview

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 the same throughout the execution of an application. You can greatly improve the readability of your code and make it easier to maintain by using constants. Use them in code that contains values that reappear or that depends on certain numbers that are difficult to remember or have no obvious meaning.

How to Create and Use Constants

Visual Basic contains a number of predefined constants, mainly using for printing and displaying. You can also create your own constants 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, which is the set of all code that can refer to it without qualifying its name, 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 defined by the object model for controls or components you work with, or they can be user-defined (that is, those you create yourself).

Compile-time and Run-time Constants

A compile-time constant is computed at the time the code is compiled, while a run-time constant can only be computed while the application is running. A compile-time constant will have the same value each time an application runs, while a run-time constant may change each time. Compile-time constants are required for cases such as array bounds, case expressions, or enumerator initializers.

See Also

Tasks

How to: Declare A Constant

Concepts

Constant and Literal Data Types

Constants Declared by Visual Basic

Reference

Const Statement (Visual Basic)

Public (Visual Basic)

Other Resources

Constants and Enumerations (Visual Basic)

Constants in Visual Basic