Variables are the memory locations which are used to store values temporarily. A defined naming strategy has
to be followed while naming a variable. A variable name must begin with an alphabet letter and should not
exceed 255 characters. It must be unique within the same scope. It should not contain any special character like
%, &, !, #, @ or $.
There are many ways of declaring variables in Visual Basic. Depending on where the variables are declared and
how they are declared, we can determine how they can be used by our application. The different ways of
declaring variables in Visual Basic are listed below and elucidated in this section.
Explicit Declaration
Using Option Explicit statement
Scope of Variables
Explicit Declaration
Declaring a variable tells Visual Basic to reserve space in memory. It is not must that a variable should be
declared before using it. Automatically whenever Visual Basic encounters a new variable, it assigns the default
variable type and value. This is called implicit declaration. Though this type of declaration is easier for the user,
to have more control over the variables, it is advisable to declare them explicitly. The variables are declared
with a Dim statement to name the variable and its type. The As type clause in the Dim statement allows to
define the data type or object type of the variable. This is called explicit declaration.
Syntax
Dim variable [As Type]
For example,
Dim strName As String
Dim intCounter As Integer
Using Option Explicit statement
It may be convenient to declare variables implicitly, but it can lead to errors that may not be recognized at run
time. Say, for example a variable by name intcount is used implicitly and is assigned to a value. In the next step,
this field is incremented by 1 by the following statement
Intcount = Intcount + 1
This calculation will result in intcount yielding a value of 1 as intcount would have been initialized to zero. This
is because the intcount variable has been mityped as incont in the right hand side of the second variable. But
Visual Basic does not see this as a mistake and considers it to be new variable and therefore gives a wrong
result.
In Visual Basic, to prevent errors of this nature, we can declare a variable by adding the following statement to
the general declaration section of the Form.
Option Explicit
This forces the user to declare all the variables. The Option Explicit statement checks in the module for usage of
any undeclared variables and reports an error to the user. The user can thus rectify the error on seeing this error
message.
The Option Explicit statement can be explicitly placed in the general declaration section of each module using
the following steps.
Click Options item in the Tools menu
Click the Editor tab in the Options dialog box
Check Require Variable Declaration option and then click the OK button
to be followed while naming a variable. A variable name must begin with an alphabet letter and should not
exceed 255 characters. It must be unique within the same scope. It should not contain any special character like
%, &, !, #, @ or $.
There are many ways of declaring variables in Visual Basic. Depending on where the variables are declared and
how they are declared, we can determine how they can be used by our application. The different ways of
declaring variables in Visual Basic are listed below and elucidated in this section.
Explicit Declaration
Using Option Explicit statement
Scope of Variables
Explicit Declaration
Declaring a variable tells Visual Basic to reserve space in memory. It is not must that a variable should be
declared before using it. Automatically whenever Visual Basic encounters a new variable, it assigns the default
variable type and value. This is called implicit declaration. Though this type of declaration is easier for the user,
to have more control over the variables, it is advisable to declare them explicitly. The variables are declared
with a Dim statement to name the variable and its type. The As type clause in the Dim statement allows to
define the data type or object type of the variable. This is called explicit declaration.
Syntax
Dim variable [As Type]
For example,
Dim strName As String
Dim intCounter As Integer
Using Option Explicit statement
It may be convenient to declare variables implicitly, but it can lead to errors that may not be recognized at run
time. Say, for example a variable by name intcount is used implicitly and is assigned to a value. In the next step,
this field is incremented by 1 by the following statement
Intcount = Intcount + 1
This calculation will result in intcount yielding a value of 1 as intcount would have been initialized to zero. This
is because the intcount variable has been mityped as incont in the right hand side of the second variable. But
Visual Basic does not see this as a mistake and considers it to be new variable and therefore gives a wrong
result.
In Visual Basic, to prevent errors of this nature, we can declare a variable by adding the following statement to
the general declaration section of the Form.
Option Explicit
This forces the user to declare all the variables. The Option Explicit statement checks in the module for usage of
any undeclared variables and reports an error to the user. The user can thus rectify the error on seeing this error
message.
The Option Explicit statement can be explicitly placed in the general declaration section of each module using
the following steps.
Click Options item in the Tools menu
Click the Editor tab in the Options dialog box
Check Require Variable Declaration option and then click the OK button
No comments:
Post a Comment