Page 92 - Open Soource Technologies 304.indd
P. 92
Event Driven Programming
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
4.3.7 Comparing Implicit and Explicit Variable Performance
The default data type for Visual Basic variables is the variant. This means that, unless you specify
otherwise, every variable in your application will be a variant. The data type is not very efficient.
Its data storage requirements are greater than the equivalent simple data type. The computer
spends more time keeping track of the data type contained in a variant than for other data types.
4.3.8 Providing Names for Your Variables
Visual Basic provides extremely liberal rules for naming variables. Briefly, all variable names
must conform to the following requirements:
• The name must being with a letter of the alphabet.
• The name must consist only of letters, digits, and the underscore character. (No punctuation
marks are allowed.)
• The name can be as long as 255 characters.
• Variable names can’t be duplicated with the same scope. This means, for example, that you
can’t have two variables of the same name within a procedure. You can, however, have two
variables with the same name in two different procedures.
4.3.9 Variables Scope and Lifetime
A variable is more than just a simple data repository. Every variable is a dynamic part of the
application and can be used at different times during the program’s execution. The declaration
of a variable establishes more than just the name and data type of the variable. Depending on
the keyword used to declare the variable and the placement of the variable’s declaration in the
program’s code, the variable might be visible to large portions of the application’s code. Alter-
natively, a different placement might severely limit where the variable can be referenced in the
procedures within the application.
The visibility of a variable or procedure is called its scope. A variable that can be seen and used
by any procedure in the application is said to have public scope. Another variable, one that is
usable by a single procedure, is said to have scope that is private to that procedure.
There are many analogies for public and private scope.Variables declared within a procedure are
local to that procedure and cannot be used or referenced outside that procedure.
In each case, the Dim keyword was used to define the variable.
86 LOVELY PROFESSIONAL UNIVERSITY