Visual Basic Const (Constant) Keyword

In visual basic, Const is a keyword and it is useful to declare constant fields in our applications. Generally, in visual basic the constant field values are set at compile-time and those values will never be changed.

 

In visual basic, if we use Const keyword to declare the constant field, then that field value cannot be changed throughout the application. So, we should not use Const keyword with the fields whose value will be changed at any time.

 

To define constant fields in visual basic, we need to use Const keyword during the declaration of fields. We can use the constants with numbers, boolean values, strings or null references.

 

It’s mandatory to initialize constant fields with required values during the declaration itself otherwise we will get compile-time errors in our visual basic application.

Visual Basic Constant Syntax

Following is the syntax of defining the constant fields using Const keyword in a visual basic programming language.

 

Const field_name As data_type = "value"

If you observe the above syntax, we used Const keyword to declare the constant variable in our application.

 

The following are the different ways of declaring and initializing the constant variables in a visual basic programming language.

 

' Constant variables

Const name As String = "Suresh Dasari"

Const location As String = "Hyderabad"

Const age As Integer = 32

If you observe the above examples, we created constant fields with different data types based on our requirements.

Visual Basic Constant Example

Following is the example of defining and using the constant fields in visual basic programming language with Const keyword.

 

Module Module1

    Sub Main()

        ' Constant variables

        Const name As String = "Suresh Dasari"

        Const location As String = "Hyderabad"

        Const age As Integer = 32

        ' This will throw compile time error

        ' name = "Rohini Alavala";

        Console.WriteLine("Name: {0}", name)

        Console.WriteLine("Location: {0}", location)

        Console.WriteLine("Age: {0}", age)

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we created constant fields with different data types and if we uncomment the commented code, we will get a compile-time error because we are trying to change the value of constant fields.

 

As discussed, once the constant field is declare and initialized, that field value must be the same throughout the application.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Constant Keyword Example Result

Visual Basic Constant Features

The following are the important features of constant variables in a visual basic programming language.

 

  • Constant fields in visual basic can be created by using Const keyword.
  • In visual basic, the constant fields must be initialized during the time of declaration.
  • Constant field values will be evaluated during the compile-time in visual basic.
  • Once values assigned to the constant fields, those values must be the same throughout the application.

This is how we can use the constant fields in visual basic programming language based on our requirements.