Const Statement (Visual Basic)
Updated: July 20, 2015
For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.
Declares and defines one or more constants.
[ <attributelist> ] [ accessmodifier ] [ Shadows ] Const constantlist
Optional. List of attributes that apply to all the constants declared in this statement. See Attribute List in angle brackets ("
<" and "
Optional. Use this to redeclare and hide a programming element in a base class. See Shadows.
Required. List of constants being declared in this statement.
constant has the following syntax and parts:
|Required. Name of the constant. See Declared Element Names.|
|Required if |
|Required. Expression that is evaluated at compile time and assigned to the constant.|
If you have a value that never changes in your application, you can define a named constant and use it in place of a literal value. A name is easier to remember than a value. You can define the constant just once and use it in many places in your code. If in a later version you need to redefine the value, the
Const statement is the only place you need to make a change.
You can use
Const only at module or procedure level. This means the declaration context for a variable must be a class, structure, module, procedure, or block, and cannot be a source file, namespace, or interface. For more information, see Declaration Contexts and Default Access Levels.
Local constants (inside a procedure) default to public access, and you cannot use any access modifiers on them. Class and module member constants (outside any procedure) default to private access, and structure member constants default to public access. You can adjust their access levels with the access modifiers.
Declaration Context. A constant declared at module level, outside any procedure, is a member constant; it is a member of the class, structure, or module that declares it.
A constant declared at procedure level is a local constant; it is local to the procedure or block that declares it.
Attributes. You can apply attributes only to member constants, not to local constants. An attribute contributes information to the assembly's metadata, which is not meaningful for temporary storage such as local constants.
Modifiers. By default, all constants are
ReadOnly. You cannot use any of these keywords when declaring a constant.
At procedure level, you cannot use
Shadowsor any access modifiers to declare local constants.
Multiple Constants. You can declare several constants in the same declaration statement, specifying the
constantnamepart for each one. Multiple constants are separated by commas.
Data Types. The
Conststatement can declare the data type of a variable. You can specify any data type or the name of an enumeration.
Default Type. If you do not specify
datatype, the constant takes the data type of
initializer. If you specify both
initializer, the data type of
initializermust be convertible to
datatype. If neither
initializeris present, the data type defaults to
Different Types. You can specify different data types for different constants by using a separate
Asclause for each variable you declare. However, you cannot declare several constants to be of the same type by using a common
Initialization. You must initialize the value of every constant in
constantlist. You use
initializerto supply an expression to be assigned to the constant. The expression can be any combination of literals, other constants that are already defined, and enumeration members that are already defined. You can use arithmetic and logical operators to combine such elements.
You cannot use variables or functions in
initializer. However, you can use conversion keywords such as
CShort. You can also use
AscWif you call it with a constant
Charargument, since that can be evaluated at compile time.
Scope. Local constants are accessible only from within their procedure or block. Member constants are accessible from anywhere within their class, structure, or module.
Qualification. Code outside a class, structure, or module must qualify a member constant's name with the name of that class, structure, or module. Code outside a procedure or block cannot refer to any local constants within that procedure or block.
The following example uses the
Const statement to declare constants for use in place of literal values.
' The following statements declare constants. Const maximum As Long = 459 Public Const helpString As String = "HELP" Private Const startValue As Integer = 5
If you define a constant with data type
Object, the Visual Basic compiler gives it the type of
initializer, instead of
Object. In the following example, the constant
naturalLogBase has the run-time type
Const naturalLogBase As Object = CDec(2.7182818284) MsgBox("Run-time type of constant naturalLogBase is " & naturalLogBase.GetType.ToString())