Is a name that represents a value that does not change during the programs execution

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:

Public Const conVersion = "07.10.A"
Const conCodeName = "Enigma"

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 * 2

Scope 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 / 2

If 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.

Is a name that represents a value that does not change during the programs execution

The general rules for constructing unique identifiers are:

  1. Names can contain letters(a-z,A-Z), digits(0-9) and underscores(_)
  2. Names must begin with a letter or an underscore (_)
  3. Names are case sensitive (testScore, testscore, Testscore are all different names)
  4. Names cannot contain whitespaces or special characters like !, #, %, etc.
  5. Reserved words (like C++ keywords, such as int) cannot be used as names

Valid and Invalid Identifers

Is a name that represents a value that does not change during the programs execution

Identifer naming conventions

  1. Variables: begin with a lowercase letter and capitalize each successive word.
  2. Constants: capitalize every letter and use underscores to separate the English words
  3. 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.

Is a name that represents a value that does not change during the programs execution

NOTE:

  1. In C++, all the variables must be declared before use
  2. A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.


Is a name that represents a value that does not change during the programs execution

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:

Is a name that represents a value that does not change during the programs execution

  1. Constants can be any of the data types.
  2. Constants name should be all uppercase with words separated by underscores (_). e.g.: TAX_RATE,

What is the name that represents a value that does not change during program execution?

A constant is a data item whose value cannot change during the program's execution. Thus, as its name implies – the value is constant.

Which means changing during the execution of the program?

Answer: Values that changes during program execution is called - Variable.

Is a name which is associated with a value that can be changed?

A variable is a symbolic name for (or reference to) information. The variable's name represents what information the variable contains. They are called variables because the represented information can change but the operations on the variable remain the same.

Is used to represent values that may be changed in program?

In programming, a variable is a value that can change, depending on conditions or on information passed to the program. Typically, a program consists of instruction s that tell the computer what to do and data that the program uses when it is running.