Variables

  • Variable is a name given to a storage area of any data value.
  • The name of the variable should be meaningful. There are some conventions which should be follow for naming variable in VB.NET:
    • Variable name can contain maximum of 255 characters.
    • User can use letters, numbers and underscore (_) character while naming variables.
    • Variable name should be start with a letter.
    • We cannot use keywords as the name of variable.

Declaring a Variable in VB.NET

  • We have to declare a variable before using it into the program.
  • The basic syntax to declare a variable in VB.NET is as follows:
  • Syntax

    [] [access-specifiers] [{ [Shared][Shadows]| [Static] }] [ReadOnly] Dim [withEvents] variableList


    A detailed explanation of above syntax is as follows:

  • attributelist : It is optional. It defines a list of attributes that apply to the variables. 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:


    variable

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

  • Static: It is optional. Static variables retain their value even after the termination of the procedure in which they are declared.
  • Shared: It is optional. Shared variables are not associated with a specific object of a class but can be shared by all objects of a class.
  • Shadows: It is optional. Shadows keyword makes a variable 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.
  • ReadOnly: It is optional. ReadOnly variables can only be read. They cannot be written.
  • Dim: It is a keyword used to declare a variable.
  • WithEvents: It is optional. This specifies that the variable is used to responds the events caused by the instance that are assigned to a variable.
  • variablelist: It is a list of declared variables.
  • Each variable in a variablelist follows the following syntax:


    variable_name[( [boundlist])][As [New] datatype][=initializer}]


    The above syntax is explained here in detail:


  • variable_name: variable is a meaningful name given to a variable. Multiple variables can be declared together in a single line separated by commas.
  • boundlist: It is a set of upper and lower bounds for each dimension of a array type variable. It is optional and only used at the time of array declaration.
  • New: It is used to create a new instance of an object immediately. It is optional.
  • datatype: It determined the data-type of a variable it can be Integer, Char, Date, Decimal etc.
  • initializer: It determined the value given to the variable at the time of declaration.

Here are some examples of variable declaration:

Dim length As Integer
Dim a As Integer, b As double, c As Char //declare variables of different-different data types in single statement
Dim a1,a2,a3 AS Integer//declare more than one variables of same data-type in single statements


Initialization of a variable in VB.NET

The syntax to initialize a variable in VB.NET is given below:

Variable_name=data_value;

You can initialize a variable at the time of declaration as given below:

Dim a1 As Integer=5

You can also initialize a variable after declaration as given below:
Dim a1 As Integer
a1=5

Working with variables

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


  • 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 Variables, and click OK as shown in figure:
  • variable

  • Now drag and drop a Button control from the toolbox to the form and change its text property to "Increment Number" as given below in the diagram:
  • variable

  • Double-click the button "IncrementNumber" to open the Button1.Click event handler. Add the following given code to it:
  • Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim number As Integer
    number = 5
    number = number + 1
    MessageBox.Show("Value of number after increment = " & number.ToString, _
    "Variables")
    End Sub


  • Now execute the project by pressing F5 key it will open a form as follows:
  • variable

  • Now click on the button "IncrementNumber" present on the form this will show you a message box as follows:
  • variable