• Have Any Question?
  • contact@eitworld.com

VB.NET Constant

Like variables constants are also used to store a data value but the value stored in a constant cannot be changed at the time of execution.


Declaring a Constant in VB.NET

Constants are also the name given to the data values stored in memory like variable but the value of constant cannot be changed at the time of execution means when we don’t want to change the value of variable or want to same value at various place then we have to use constants. It is used to represent the fixed value which cannot be changed at runtime or cannot vary in the program after assigning the value to it at first time.

    • We have to declare a constant before using it into the program.
    • The basic syntax to declare a constant in VB.NET is as follows:
[] [access-specifiers] [Shadows] Const constantlist

A detailed explanation of above syntax is as follows:


    • attributelist: It is optional. It defines a list of attributes that apply to the constants. Multiple attributes are seprated by commas.

Each attribute in a attribute list follows given syntax:


Each part of the above syntax is explained below in the table:


    • access-specifier: It is also optional. access-specifiers determined the visibility of a constant in a program. These can be: Public, Protected, Friend, Private and Protected Friend. These access-specifiers are explained below in the following table:


    • Shadows: It is optional. Shadows keyword makes a constant a shadow of an identically named programming element in a base class and an element that is modified with keyword Shadow is unavailable in the derived class that shadow it.
    • Const:Const is a keyword used to declare a constant.
    • constantlist:It is the list of the declared constants.

Each constant in constantlist must use the following syntax:


name [As type] = initexpr

    • name: It is the name of the constant. We can declare more than one constant in a statement. Multiple constants are separated by the commas.
    • type: It determines the data type of a constant. It can be integer, float, double, char etc.
    • initexpr: It is an initialization expression. Constant must be initialize at the time of declaration.

Here is an example of constant declarations


Const a1 As Integer= 5

Working with Constant


Here we are going to create a project in which we are using a constant:


    • Create a new project in Visual Studio 2010 by selecting File -> New Project from the menu bar. In the New Project dialog, select Windows Forms Application from the right-hand pane, enter the project name as Constants, and click OK as shown in figure:


    • Now drag and drop a Button control from the toolbox to the form and change its text property to “ShowConstantValue” as given below in the diagram:


    • Double-click the button “ShowConstantValue” to open the Button1.Click event handler. Addthe following given code to it:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Const pi As Double = 3.14
MessageBox.Show(“Constant Value of pi = ” & pi.ToString, _
End Sub
    • Now execute the project by pressing F5 key it will open a form as follows:


    • Now click on the button “ShowConstantValue” present on the form this will show you a message box as follows:


Real Time Web Analytics