VB.Net

VB.Net Constants

VB.Net Constants

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.

Top course recommendations for you

    Dev C++ Tutorial
    1 hrs
    Beginner
    6.4K+ Learners
    4.45  (247)
    Hashing in Java
    2 hrs
    Intermediate
    6.6K+ Learners
    4.45  (202)
    Eclipse Tutorial
    1 hrs
    Beginner
    3K+ Learners
    4.51  (148)
    NoSQL Database
    2 hrs
    Beginner
    12.4K+ Learners
    4.51  (1023)
    SQL Triggers for Beginners
    1 hrs
    Intermediate
    13K+ Learners
    4.48  (615)
    WordPress with Docker
    1 hrs
    Beginner
    9.1K+ Learners
    4.52  (527)
    C++ Tutorial
    2 hrs
    Beginner
    49.5K+ Learners
    4.42  (3057)
    OOPs Concepts in C++
    2 hrs
    Beginner
    31.4K+ Learners
    4.42  (2167)
    Robotic Process Automation
    3 hrs
    Beginner
    13.4K+ Learners
    4.6  (759)
    Docker Swarm
    1 hrs
    Beginner
    1.8K+ Learners
    4.5  (126)