VB.net Constants – How To Declare a Constant in VB.net

What is Constants in VB.net?

The VB.net Constants are fixed values that the program cannot change while it is running.

Literals are another name for these fixed values.

Integer constants, floating constants, character constants, and string literals are all examples of basic data types.

Enumeration constants are also present.

Constants in VB.net are processed similarly to ordinary variables, with the exception that their values cannot be changed after they have been defined.

These constants can be of any data type, such as Integer, Double, String, Decimal, Single, character, enum, etc.

An enumeration is a set of named integer constants.

VB.net Constant Declaration

Constants in VB.net are declared using the Const statement.

The Const statement is used at module, class, structure, procedure, or block level for use in place of literal values.

The syntax for the Const statement is:

'syntax for the const'

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

1. Attributelist

Attributelist − specifies the list of attributes applied to the constants; you can provide multiple attributes separated by commas. Optional.

2. Accessmodifier

Accessmodifier − specifies which code can access these constants. Optional. Values can be either of the: Public, Protected, Friend, Protected Friend, or Private.

3. Shadows

Shadows − this makes the constant hide a programming element of an identical name in a base class. Optional.

4. Constantlist

Constantlist − gives the list of names of constants declared. Required.

each constant name has the following syntax and parts:

'constants name syntax'

constantname [ As datatype ] = initializer

1. constantname

constantname − specifies the name of the constant

2. datatype

datatype − specifies the data type of the constant

3. initializer

initializer − specifies the value assigned to the constant

For Example:

'The following statements declare constants.'

Const maxval As Long = 4999
Public Const message As String = "HELLO ITSOURCECODERS" 
Private Const piValue As Double = 3.1415

Example Code of Constants Value

The following example demonstrates the declaration and use of a constant value

'Example Code of Constants Value'

Module constantsNenum
   Sub Main()
      Const PI = 3.14149
      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 result:

Area = 153.933

You can test the above example here! ➡ VB.net Online Compiler

Print and Display Constants in VB.net

VB.net provides the following print and display constants in VB.net.

Sr.No.Constant & Description in VB.net
1.vbCrLf Carriage return/linefeed character combination.
2.vbCr Carriage return character.
3.vbLf Linefeed character.
4.vbNewLine Newline character.
5.vbNullChar Null character.
6.vbNullString Not the same as a zero-length string (“”); used for calling external procedures.
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.
Following Print and Display Constants in VB.net

Declaring Enumerations in VB.net

An enumerated type is declared using the Enum statement. The Enum statement declares an enumeration and defines the values of its members.

The Enum statement can be used at the module, class, structure, procedure, or block level.

The syntax for the Enum statement is as follows:

'syntax for enum statement'

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

1. attributelist

attributelist − refers to the list of attributes applied to the variable. Optional.

2. accessmodifier

accessmodifier − specifies which code can access these enumerations. Optional. Values can be either of the: Public, Protected, Friend,ly or Private.

3. Shadows

Shadows − this makes the enumeration hide a programming element of an identical name in a base class. Optional.

4. enumerationname

enumerationname − name of the enumeration. Required

5. datatype

datatype − specifies the data type of the enumeration and all its members.

6. memberlist

memberlist − specifies the list of member constants being declared in this statement. Required.

Each member in the memberlist has the following syntax and parts:

1. name

name − specifies the name of the member. Required.

2. initializer

initializer − the value assigned to the enumeration member. Optional.

For Example:

'Example of Memberlist'

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

Example Code for Declaration and Use of The Enum Variable Colors

The following example demonstrates the declaration and use of the Enum variable Colors.

'Example Code for Declaration and Use of The Enum Variable Colors'

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 result:

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

You can test the above example here! ➡ VB.net Online Compiler

Summary

Const values help clear up confusing code. They influence performance often in a positive way. With Const we construct self-documenting code.

A Const with the identifier “name” is known to be a name by any programmer reading the code.


Leave a Comment