Skip to main content
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
User-Defined Constants (Visual Basic)
- Article
- 09/15/2021
- 2 minutes to read
In this article
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 can use constants that are defined by the controls or components you work with, or you can create your own. Constants you create yourself are described as user-defined.
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.
Const Statement Usage
A Const statement can represent a mathematical or date/time quantity:
Const conPi = 3.14159265358979 Public Const conMaxPlanets As Integer = 9 Const conReleaseDate = #1/1/1995#It also can define String constants:
The expression on the right side of the equal sign ( = ) is often a number or literal string, but it also can be an expression that results in a number or string (although that expression cannot contain calls to functions). You can even define constants in terms of previously defined constants:
Const conPi2 = conPi * 2Scope of User-Defined Constants
A Const statement's scope is the same as that of a variable declared in the same location. You can specify scope in any of the following ways:
To create a constant that exists only within a procedure, declare it within that procedure.
To create a constant available to all procedures within a class, but not to any code outside that module, declare it in the declarations section of the class.
To create a constant that is available to all members of an assembly, but not to outside clients of the assembly, declare it using the Friend keyword in the declarations section of the class.
To create a constant available throughout the application, declare it using the Public keyword in the declarations section the class.
For more information, see How to: Declare A Constant.
Avoiding Circular References
Because constants can be defined in terms of other constants, it is possible to inadvertently create a cycle, or circular reference, between two or more constants. A cycle occurs when you have two or more public constants, each of which is defined in terms of the other, as in the following example:
Public Const conA = conB * 2 Public Const conB = conA / 2If a cycle occurs, Visual Basic generates a compiler error.
See also
- Const Statement
- Constant and Literal Data Types
- Constants and Enumerations
- Constants and Enumerations
- Enumerations Overview
- Constants Overview
- How to: Declare an Enumeration
- Enumerations and Name Qualification
- Option Strict Statement
Feedback
Submit and view feedback for
Identifiers, Variables, and Constants
Identifiers
An identifier is an unique name given to an entity, which distinctly identifies an entity in a program at the time of its execution. Identifiers are used for the naming of variable, function, class, structure or a constant, etc. Once an identifier is declared, we can use the identifier anywhere in the program to refer to the associated value.
The general rules for constructing unique identifiers are:
- Names can contain letters(a-z,A-Z), digits(0-9) and underscores(_)
- Names must begin with a letter or an underscore (_)
- Names are case sensitive (testScore, testscore, Testscore are all different names)
- Names cannot contain whitespaces or special characters like !, #, %, etc.
- Reserved words (like C++ keywords, such as int) cannot be used as names
Valid and Invalid Identifers
Identifer naming conventions
- Variables: begin with a lowercase letter and capitalize each successive word.
- Constants: capitalize every letter and use underscores to separate the English words
- Programmer written functions: will be capitalized in the same way as variable names but they will begin with a capital letter.
Variables
A variable is a named storage location in the computer's memory for holding a piece of information. The information stored in variables may change while the program is running (hence the name "variable"). Variables are symbolic names that represent locations in the computer's random-access memory (RAM). When information is stored in a variable, it is actually stored in RAM.
Syntax
data_type variable_name = value;
Variable declaration
Take a look at this: int age; This is called variable declaration. It tells the compiler the variable's name and the data type it will hold. Memory is allocated upon variable declaration.
This line indicates the variable's name is age. The word int stands for integer, so age will only be used to hold integer numbers. The data type also determines the amount of memory space to be allocated for the variable.
Variable declaration and initialization
Take a look at this: int age = 20; This is called variable declaration and initialization. It provides a inital value 20 to age, which will be saved in the allocated memory location.
Usually, variable declaration and initialization are done together.
NOTE:
- In C++, all the variables must be declared before use
- A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.
Constants refer to as fixed values, unlike variables whose value can be altered, constants - as the name implies does not change, they remain constant. Constant must have to be initialized at the time of creating it, and new values cannot be assigned later to it.
Defining Constants Syntax
const data_type variable_name = value;
Example:
- Constants can be any of the data types.
- Constants name should be all uppercase with words separated by underscores (_). e.g.: TAX_RATE,