Variable are those value which take place in the memory and called with a name.a variable is a storage area of the computer's memory. Think of it like this:a variable is an empty cardboard box.Now, imagine you have a very large room,and in this room you have a whole lot of empty cardboard boxes. Each emptycardboard box is a single variable. To add two numbers together, write the first number on a piece of paper and put the piece of paper into an empty box.Write the second number on a piece of paper and put this second piece of paper in a different cardboard box.
Types of variable:
In Visual Basic there are five type of variable of following categories:
The two major variable categories are numeric and string. numeric variables stores numbers
and string variable stores text
Visual Basic language provide a variety of numeric data type.
- Integer (there are several integer data type)
- Single (or floating point number with decimal precision)
- Double (or floating point number with extreme precision)
| Memory representation
| 2 Bytes
||integer values in range-32,768 to 32,767
| 4 Bytes
|| integer values in range -2,147,483,648 to 2,147,483,647.
| 8 Bytes
||Very large Integer values.
Double-precision floating-point number. it can represent negative. numbers in the range -1.79769313486232e308 to -4.94065645841247e and positive number in the range 4.94065645841247e-324 to 1.79769313486232e308.
| 16 Bytes
|| Integer and floating point number scaled by a factor in the range from 0 to 28.
The numeric Integer variable in Visual Basic:
Dim number1 As Integer = 3
Dim number2 As Integer = 5
Dim answer As Integer
answer = number1 + number2
Dim number1 As Integer, number2 As Integer, answer As Integer
String data type variable stores only text, and String type variables are declared with the String type:
example shows the declaration of VISUAL BASIC .net variables.
Dim firstname As String = "rohatash"
Dim lastname As String = "kumar"
Dim fullname As String
fullname = firstname + lastname
Short for Dimension. It's a type of variable. You declare (or "tell" Visual Basic) that you are setting up a variable with this word. We'll
meet other types of variables later, but for now just remember to start your variable declarations with Dim.
This is the cardboard box and the sticky label all in one. This is a variable. In other words, our storage area. After the Dim word, Visual Basic is
looking for the name of your variable. You can call your variable almost anything you like, but there are a few reserved words that VB won't allow.
It's good practice to give your variables a name appropriate to what is going in the variable.
We're telling Visual Basic that the variable is going to be a number (integer). Well meet alternatives to Integer later.
The Boolean data type stores true and false values. Boolean variables are, in essence, integers that take the value
1(for true) and 0(for false). actually, any non-zero values is considered true.
Boolean variable declaration:
Dim read As Boolean
A simple example of Boolean variable:
Dim running As Boolean
If running = True Then
running = False
running = True
Variant-Variables without a fixed data type. The variants are the opposite of strictly typed variables,
they can store all types of values, from a single character to an object.
Object variable define as following:
Dim myvar As Object
Date variable are used to store date and time in a special format.
A variable declared as date can store both date and time values with a statement
like this following:
Dim expiration As Date
The following are all valid assignment:
expiration = #01/01/2004#
expiration = #8/27/2001 6:29:11 pm#
expiration = " July 2,2002"
expiration = now()
For example :
Dim days As Date
days = Now()
In above example now() shows the current time and date.
Hope that article would have helped you in understanding variable and their importance in coding. Proper usage of Variables makes your code easily to understand.