Variable and it's declaration
Variable:
Variable is considered as a place in which a value can be stores. accessed and retrieved in most languages. Variable shall be declared for the compiler before it is used like C-language etc. Visual Basic does not force the programmer to declare all variables. However a Variable can be declared implicitly or explicitly in Visual Basic.
Implicit Declaration:
As mentioned above Visual Basic do not force a programmer to declare all Variables. If a Variable is used without explicit Variable is considered variant e.g.
Dim var1, var2 var1 and var2 are variant Variable.
Var1 = "Ali"
Var2 = 20
Var1 is considered as String and Var2 considered as numeric by the Visual Basic. To know about type of Variable if it declared as Variable variant, then you can use TypeName function e.g.
Print "Type of Var1 is " & YypeName (var1)
The output of above statement will be ' Type of var1 is string' . Suffix can be used with Variable's name to know about it's type. Suffix for different types are different such as.
Symbol
|
Data Type
|
Example
|
$
|
String
|
Name$ = “Ishaaq”
|
%
|
Integer
|
Marks% = 433
|
&
|
Long
|
Distance& = 234566
|
!
|
Single
|
Average! = 67.8
|
#
|
Double
|
Accuracy# = 345.34343
|
Variable can be declared by using the Defxxx Statement. e.g.
DefInt a-b
In the above all Variables whose first character is either a or b is considered as Integer.
other list is Defbool, DefByte, DefInt, DefLng, DefCur, DefSng, Defdabl, DefDate, DefStr, and DefVar.
Explicit Declaration:
A Variable can be declared explicitly by using Dim statement e.g.
Dim Age As Integer
Dim Name As String
More than one variable can be declared with one Dim statement like the following.
Dim a As Integer, b As Single
If you declare two Variables with one Dim like the following.
Dim a, b As Integer
Then type of Variable a will be Variant ang type of Variable b will be integer.
Rules For Naming Variables:
Following main Rules must be followed strictly at the time declaring Variables.
- Variable name must begin with letter or under score.
- Length of the name of variable must not exceed 255 characters an any case.
- Variable must be unique within its scope i.e. one Variable cannot be named and declared twice within its scope.
- Reserve words cannot be used as a name of a variable.
Me : If you need more help, want to ask feel free to use below comment box. It will be my pleasure to help you. THANKS for reading.
0 comments:
Post a Comment