When you are programming, you will experience a situation where the code contains values that recur. And the code uses numbers that are hard to remember, and those numbers don't have a clear meaning. In this case, you can use constants to easily improve the readability and maintainability of your code. Constants are the use of meaningful names to replace frequently used values or strings. Although a constant is a bit like a variable, it cannot be modified like a variable, that is, it cannot be assigned again after the initial value of the Chang. There are two sources of constants:
1. Internal or system-defined constants are provided by applications and controls. These constants are defined in the Visual Basic Object library.
2. User-defined constants are declared using the Const statement.
Constants from the Visual Basic Object library consist of the following forms:
Namespaces1.namespaces2.....constname, for example:
Microsoft.VisualBasic.MsgBoxStyle.OKOnly is a constant in the Visual Basic dialog box with a value of 0.
First, custom constants:
The syntax for declaring constants is:
[public| private| protected| friend| Protected Friend] Const constname [as Type]=expression
The parameter "Constname" is a valid symbol name, and "expression" consists of numeric constants or string constants and operators, but function calls cannot be used in "expression". The Const statement can represent the quantity, date, and time:
Const conpi=3.14159265358979
Public Const Conmaxplanets as integer=9
Const conreleasedate= #1/1/95#
You can also define string constants with the Const statement:
Public Const conversion= "07.10.A"
Const concodename= "Enigma"
If separated by commas, multiple constant declarations can be placed on one line:
Public Const conpi=3.14,conmaxplanets=9,conworldpop=6e+09
The left side of the equal sign must be the left value, and the expression to the right of the equal sign is usually a number or literal string, but it can also be an expression whose result is numeric or string (although the expression cannot contain a function call), and even the new constant can be defined with a constant defined previously.
Const conpi2=conpi*2
When a constant is defined, it can be placed in code to make the code more readable. For example:
Const conpi=3.14
Area=conpi*dblr^2
Second, avoid circular references
Because constants can be defined with other constants, it is important to be careful not to have loops or circular references between more than two constants. A loop occurs when there are more than two common constants in the program, and each constant is defined with another, for example:
' In the Module1:
Public Const cona=conb*2 ' is valid throughout the application
' In the Module2:
Public Const CONB=CONA/2 ' is valid throughout the application
If a loop occurs, Visual basic generates an error message when you try to run the application. You cannot run a program without resolving a circular reference. To avoid loops, you can limit the public constants to a single module, or at most only a few modules.