Constants Overview (Visual Basic)

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.


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.

In This Section

Definition Term
How to: Declare A Constant Explains how to use the Const statement to declare a constant and set its value; by declaring a constant, you assign a meaningful name to the value.
User-Defined Constants Describes how to create your own constants, including information on scoping and how to avoid circular references.
Constant and Literal Data Types Provides information on how the Visual Basic compiler initializes constants when Option Explicit is turned off.
How to: Group Related Constant Values Together Demonstrates how to group constant values that are related.


Definition Term
Constants and Enumerations Lists the constants predefined by Visual Basic.
Const Statement Describes the Const statement and its use.
Option Strict Statement Describes the Option Strict statement and its use.

See also