Constants are fixed values that are not changed during the execution of the program. These fixed values are also referred to as literals.
Constants can be of any basic data kinds, such as integers, characters, floating points, or string literals. Enumeration constants are also available.
Constants are processed similarly to regular variables, except that their values cannot be changed after they are defined.
A set of named integer constants is called an enumeration.
Declaring Constants
Constants are declared in VB.Net using the Const statement. The Const statement replaces literal values at the module, class, structure, procedure, or block level.
“Const statement” syntax is as follows −
[ < attributelist > ] [ accessmodifier ] [ Shadows ]
Const constantlist
Where,
- attributelist − indicates the set of attributes applied to the constants; Multiple attributes can be specified, separated by commas. Optional.
- accessmodifier − defines which code is allowed to access these constants Optional. Public, Protected, Friend, Protected Friend, or Private values are all possible.
- Shadows − This causes the constant to conceal a programming element with the same name in a base class. Optional.
- Constantlist – is the list of names of declared constants. Required.
The syntax and parts of constants are as follows−
constantname [ As datatype ] = initializer
- constantname − the name of the constant
- datatype − the data type of the constant
- initializer − the value assigned to the constant
Print and Display Constants in VB.Net
Below is a list of VB.Net ‘print and display’ constants −
Sr.No. |
Constant & Description |
1 |
vbCrLf A carriage return/linefeed character combination. |
2 |
vbCr Carriage return character. |
3 |
vbLf Linefeed character. |
4 |
vbNewLine Newline character. |
5 |
vbNullChar Null character. |
6 |
vbNullString It is used for calling external procedures and is different from a zero-length string (""). |
7 |
vbObjectError Error number. User-defined error numbers should be greater than this value. For example: Err.Raise(Number) = vbObjectError + 1000 |
8 |
vbTab Tab character. |
9 |
vbBack Backspace character. |
Declaring Enumerations
Enum statement is used to declare enumeration type and define the value of its members. It can be used at the class, module, procedure, structure, or block level.
The syntax for the Enum statement is as follows −
[ < attributelist > ] [ accessmodifier ] [ Shadows ]
Enum enumerationname [ As datatype ]
memberlist
End Enum
Where,
- attributelist: refers to the list of attributes that have been applied to the variable. Optional.
- Accessmodifier: determines which code is permitted to access these enumerations. Optional. Values can be classified as Public, Protected, Friend, or Private.
- Shadows: This causes the enumeration to hide a programming element with the same name in a base class. Optional.
- Enumerationname: is the enumeration's name. Required data type indicates the enumerations and all of its members' data types.
- Memberlist: denotes the list of member constants defined in this statement. Required.
Syntax and parts of members in the ‘memberlist’:
[< attribute list >] member name [ = initializer ]
Where,
- name − defines the name of the member. Required.
- initializer – is the value assigned to the member of the enumeration. Optional.