A constant is a constant in the process of running the program. Since some constants are often used repeatedly during programming and development, and their meanings are sometimes difficult to remember, we need to check each time. If constants are defined, they will simplify them, this improves code readability and maintainability.
Note: you cannot arbitrarily modify or assign new values.
VB. there are two types of constants in. NET, which are customized by the user using the Const statement or pre-defined by the system, you can use the "View", "other Windows", and "Object Browser" commands in the menu, or press Ctrl + Alt + J to Open VB. NET 'and the constant list in the VBA Object Library. These constants can be used with the objects, methods, and attributes of the application, and the Object Library of each ActiveX Control also defines separate constants.
The syntax format for declaring constants is as follows:
Reference content is as follows: [Public | Private | Friend l Protected | Pr0 tected Friend | Const constant name [As type] = expression |
Constant name: Any valid symbol name.
Expression: it consists of characters, numeric constants, and operators. It can also be a numeric or string expression. However, the expression cannot be called using a function. NOTE: If Option strict is enabled in the program, the statement must explicitly declare the constant.
A Const statement can be used to declare constants of quantity, String, time, or date. For example:
Reference content is as follows: Const pi = 3.14159265358979323846 Private Const sum as maid = 1000 |
. Public, Private, Protected, and Friend are still used for the declaration of Enumeration type scopes. For example:
Reference content is as follows: Private Enum months Public Enum months Protected Enum months Friend Enum months Protected Friend Enum months |
By default, the first constant in www.zxbc.cn enumeration is initialized to O,
The subsequent values are 1 greater than the previous constants. You can also assign values to Constants by using the value assignment statement. This value can be any value, including negative numbers. For example:
Reference content is as follows: Public Enum halfyear January February = O Marth Else l May June Invalid = 1 End Enum |
In the above Code, Invalid is specified as 1 and February is specified as 0. However, since January is the first constant of enumeration, its initial value is 0. The value of Marth should be 1 (1 larger than February), and so on. In addition, the default enumerated value is Integer, but it can also be explicitly declared as the required type (except the floating point type) When referencing the enumerated member, generally, you must use an enumeration name, but use VB. the Import Statement provided in. NET can avoid using a full qualified name. After the Import Statement is introduced, reference with ambiguous meanings must be avoided. Note that multiple enumerations may contain the same constants.