Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

VB.Net - Constant and enumeral


May 13, 2021 vb.net


Table of contents


Constants constants are fixed values that the program may not change during execution. These fixed values are also known as text.

Constants can be any basic data type, such as integer constants, floating-point constants, character constants, or string constants. There are also enumerumered constants.

Constants are treated as regular variables, except that their values cannot be modified after their definition.

Enumeration enumeration is a set of named integer constants.


Declares a constant

In VB.Net, the constant is declared using the Const statement. Const statements are used at the module, class, structure, procedure, or block level to replace text values.

The syntax of the Const statement is:

[ < attributelist> ] [ accessmodifier ] [ Shadows ] 
Const constantlist

1, attributelist: specify the list of properties applied to constants; Y ou can provide multiple properties separated by commas. Optional.

2, accessmodifier: Specify which code can access these constants. O ptional. Values can be: Public, Protected, Friend, Protected Friend, or Private.

3, Shadows: This allows the constant to hide programming elements of the same name in the base class. Optional.

4, Constantlist: Gives a list of the names of the constants declared. Required.


Each constant name has the following syntax and sections:

constantname [ As datatype ] = initializer

1, constantname constant name: specify the name of the constant


2, data type data type data type: specify the data type of the constant


3, initializer initial value setting: specify the value assigned to the constant


For example

'The following statements declare constants.'
Const maxval As Long = 4999
Public Const message As String = "HELLO" 
Private Const piValue As Double = 3.1415


Example

The following example demonstrates the declaration and use of constant values:

Module constantsNenum
   Sub Main()
      Const PI = 3.14159
      Dim radius, area As Single
      radius = 7
      area = PI * radius * radius
      Console.WriteLine("Area = " & Str(area))
      Console.ReadKey()
   End Sub
End Module


When the above code is compiled and executed, it produces the following results:

Area = 153.938


Output VB.Net display constants in the output

Vb. N et provides the following print and display constants:

Constant Describe
vbCrLf Carriage return/lineback character combination.
vbCr Enter the character.
vbLf Line-by-line characters.
vbNewLine Line-by-line characters.
vbNullChar Empty character.
vbNullString does not equal the zero-length string (""); Used to call an external procedure.
vbObjectError Error number. T he user-defined error number should be greater than this value. F or example:
Err.Raise (numbers) - vbObjectError - 1000
vbTab The label character.
vbBack The back-to-back character.


Declaration enumerity

Use the Enum statement to declare the enumerus type. T he Enum statement declares an enumeration and defines the value of its members. Enum statements can be used at the module, class, structure, procedure, or block level.

The syntax of the Enum statement is as follows:

[ < attributelist > ] [ accessmodifier ]  [ Shadows ] 
Enum enumerationname [ As datatype ] 
   memberlist
End Enum

1, attributelist: refers to the list of properties applied to variables. Optional.

2, asscesmodifier: Specify which code can access these enumerations. O ptional. Values can be: public, protected, friend or private.

3, Shadows: This allows enumeraling to hide programming elements with the same name in the base class. Optional.

4, enumerationname: Enumeration of the name. Required

5, datatype: specify the enumeration of the data type and all its members.

6, memberlist: Specifies a list of member constants declared in this statement. Required.


Each member in the member list has the following syntax and sections:

[< attribute list>] member name [ = initializer ]
  • name name: Specifies the name of the member. R equired.

  • initializer initialization: The value assigned to the enumerated member. O ptional.


For example

Enum Colors
   red = 1
   orange = 2
   yellow = 3
   green = 4
   azure = 5
   blue = 6
   violet = 7
End Enum


Example

The following example demonstrates the declaration and use of the Enum variable color:

Module constantsNenum
   Enum Colors
      red = 1
      orange = 2
      yellow = 3
      green = 4
      azure = 5
      blue = 6
      violet = 7
   End Enum
   Sub Main()
      Console.WriteLine("The Color Red is : " & Colors.red)
      Console.WriteLine("The Color Yellow is : " & Colors.yellow)
      Console.WriteLine("The Color Blue is : " & Colors.blue)
      Console.WriteLine("The Color Green is : " & Colors.green)
      Console.ReadKey()
   End Sub
End Module


When the above code is compiled and executed, it produces the following results:

The Color Red is: 1
The Color Yellow is: 3
The Color Blue is: 6
The Color Green is: 4