You are currently viewing Types Of Variables In VB.Net 2017 variable

Types Of Variables In VB.Net 2017

In every programming languages the developer use the different types of variables to define the data type. Basically a variable is nothing . In VB.Net It take a minimum storage or size  in database type and layout. The range of values is stored in variables within a memory. The operation are applied in class , function and events.

There are two way to declare the variable either public or private. The public variable can be called in any form in the projects and private variable only called in a form which you have declared.


DIM Student_id AS integer
Student_id = 10

when we call the above variable Student_id in any event it is automatically listed visual studio editor. You can use it to assign the value.You can declare multiple variable in a single data type like this .

types of variables

The Dim statement use to declare one or more variables in a single statement.

Thi Dim statement is used in module, class, structure and procedure. 

Syntax of variables declare in is

  • Attribute-list: is use in variables it is optional.
  • Accessmodifier:  It is define to the access level of variables , It has values public, protected, friends and private it is optional.
  • Shared : It is not associate a specific instance rather it can call in any instance class, module, function and single events. 
  • Shadows: It indicate the re-declared in instance.
  • Static: It is use after termination of procedure of instance.
  • ReadOnly: It means variable is declare but not use in instance.
  • WithEvents:  It is declare in a specific events and use in it.
  • VariableList: It is the set of variables lists.


VB.NET Windows Overview

types of variables

How To Add Module In VB.Net

Step 01: Click on project in the menu choose the add module in menu list.

types of variables

Step 02: Enter the name of module for example Public variable and click on add button.

types of variables

How to declare Public variables?

types of variables

Some of essential VB.Net Variable are :-

types of variables

Public Class Form1
Dim Student_id As Integer
Dim First_name, Last_name As String
Dim dob As Date
Dim age As Int16
Dim status As Boolean
Dim fee As Double

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Student_id = “1”
First_name = “SADAQAT”
Last_name = “HUSSAIN”
dob = “25/11/2008”
age = “12”
status = “ACTIVE”
fee = “1500”

End Sub

Call A Variables Through Button Click Event

Step 01 : Drap a button control from toolbox into Form.

Step 02 : Double click on the button or right click on it and choose the view code or press f7, the code window will open.

Step 03 : Add the following code on button in click event.

types of variables

types of variables

types of variables

Private Sub btnplus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnplus.Click

Dim student_status As Boolean
student_status = true
MsgBox (“The value of variable student_status is : “& student_status )

Dim student_name As String
student_name = “Salman Ahmed”
MsgBox (“The value of variable student_name is : “& student_name )

Dim DOB As Date
DOB= “01/05/2017”
MsgBox (“The value of variable DOB is :” & DOB)

Dim fee As Double
DOB= “1500”
MsgBox (“The value of variable fee is :” & fee)

End Sub

types of variables:

types of variables

After declare the variables on button click . Click on the run button to run the project after successfully build the program click on button you will see the following messages one by one on you screen which you have declare in variable.

types of variables

Uses of String Variables:

In VB.Net types of variables the  string variables are use to in query select query for select the record  from the data base to catch the duplicate record number. The string variables are use to insert or delete statement from the database tables. Following are the example of string variables to operate some essential queries.

In Types of Variables, The String Variable Also Use In Select Statement:

con = New OleDbConnection(cs)
Dim ct As String = “select ID from Customer where ID=@find”

cmd = New OleDbCommand(ct)
cmd.Connection = con
cmd.Parameters.Add(New OleDbParameter(“@find”, System.Data.OleDb.OleDbType.VarChar, 20, “ID”))
cmd.Parameters(“@find”).Value = txtCustomerID.Text
rdr = cmd.ExecuteReader()

If rdr.Read Then
MessageBox.Show(“Customer ID Already Exists”, “Input Error”, MessageBoxButtons.OK, MessageBoxIcon.Error)

If Not rdr Is Nothing Then
End If


String Variable Use In Insert Statement:

con = New OleDbConnection(cs)

Dim cb As String = “insert into Customer(ID,name,address,city,Phone,email,mobileno,notes) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8)”

cmd = New OleDbCommand(cb)

cmd.Connection = con

cmd.Parameters.Add(New OleDbParameter(“@d1”, System.Data.OleDb.OleDbType.VarChar, 20, “CustomerID”))
cmd.Parameters.Add(New OleDbParameter(“@d2”, System.Data.OleDb.OleDbType.VarChar, 100, “name”))
cmd.Parameters.Add(New OleDbParameter(“@d3”, System.Data.OleDb.OleDbType.VarChar, 250, “address”))

cmd.Parameters.Add(New OleDbParameter(“@d4”, System.Data.OleDb.OleDbType.VarChar, 50, “city”))

cmd.Parameters.Add(New OleDbParameter(“@d5”, System.Data.OleDb.OleDbType.VarChar, 15, “phone”))

cmd.Parameters.Add(New OleDbParameter(“@d6”, System.Data.OleDb.OleDbType.VarChar, 150, “email”))

cmd.Parameters.Add(New OleDbParameter(“@d7”, System.Data.OleDb.OleDbType.VarChar, 15, “mobileno”))

cmd.Parameters.Add(New OleDbParameter(“@d8”, System.Data.OleDb.OleDbType.VarChar, 250, “notes”))

cmd.Parameters(“@d1”).Value = txtCustomerID.Text
cmd.Parameters(“@d2”).Value = txtName.Text

cmd.Parameters(“@d3”).Value = txtAddress.Text

cmd.Parameters(“@d4”).Value = cmbCity.Text

cmd.Parameters(“@d5”).Value = txtPhone.Text

cmd.Parameters(“@d6”).Value = txtEmail.Text

cmd.Parameters(“@d7”).Value = txtMobileNo.Text

cmd.Parameters(“@d8”).Value = txtNotes.Text

MessageBox.Show(“Successfully saved”, “Customer Details”, MessageBoxButtons.OK, MessageBoxIcon.Information)
Save.Enabled = False
If con.State = ConnectionState.Open Then
End If

End If

Catch ex As Exception
MessageBox.Show(ex.Message, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try