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:
- Numeric
- String
- Boolean
- Date
- Object
The two major variable categories are numeric and string. numeric variables stores numbers
and string variable stores text
Numeric variable:
Visual Basic language provide a variety of numeric data type.
- Integer (there are several integer data type)
- Decimal
- Single (or floating point number with decimal precision)
- Double (or floating point number with extreme precision)
Data type
|
Memory representation |
Stores |
Short(Int16)
|
2 Bytes |
integer values in range-32,768 to 32,767 |
Integer(Int32)
|
4 Bytes |
integer values in range -2,147,483,648 to 2,147,483,647. |
long(Int64)
|
8 Bytes |
Very large Integer values. |
Double |
8bytes |
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.
|
decimal
|
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
Console.WriteLine(answer)
Dim number1 As Integer, number2 As Integer, answer As Integer
String variables:
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
Console.WriteLine(fullname)
Dim:
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.
Number1:
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.
As Integer:
We're telling Visual Basic that the variable is going to be a number (integer). Well meet alternatives to Integer later.
Boolean variable:
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
Console.WriteLine("false")
Else
running = True
Console.WriteLine("true")
Object Variable:
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 variables:
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()
Console.WriteLine(days)
In above example now() shows the current time and date.
Conclusion:
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.