Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 4: Data, Operations, and Built-In Functions
Last change: February 13 2016 18:47:36.
<<PreviousNext>>

Scope and Lifetime: A Recap

To recapitulate, variables have different scopes and lifetimes, depending on where and how they are declared. A variable declared at the form (class) level is recognized by all procedures; a variable declared in a procedure is recognized only in that procedure. If the same variable name is declared in different contexts (in the form and/or in different procedures), the variable declared in the procedure is the one recognized inside the procedure.
Besides its scope, a variable also has its lifetime. A form level variable has its value preserved until the form is destroyed. The value of a procedure level variable is reinitialized each time the procedure is called if the variable is declared with a Dim statement. If it is declared with a Static statement, however, the procedure level variable’s value is preserved until the form is destroyed.
The following table provides a summary of the scope and duration of variables.

Declaration Scope Duration
Dim A As Integer (in the general declaration area) Form (class) level variable recognized by all procedures The value is preserved for as long as the form exists.
Dim B As Integer (inside a procedure) Procedure level (local) variable recognized only in that procedure As soon as the procedure ends, the value is gone.
Dim C As Integer (inside a block) Block level variable recognized only in that block As soon as the execution control leaves the block, the name is not recognized.
Static D As Integer (allowed only inside a procedure) Procedure level (local) variable recognized only in that procedure The value is preserved for as long as the form exists.

Declaring Constants

The value of named constants cannot be changed. Although they have similar scope and lifetime as variables, it would seem more appropriate to use the same names for the same types and values throughout the project. One potential problem is that broad scope constants declared at different places in a project may result in name collision; same names have different values and types. To guard against such a problem, declare all constants in one place in your program. This practice enhances the consistency and maintainability for the project.

Additional Note on Declaration

The preceding discussion on declaring variables and constants dealt with projects with only one form. Will there be any difference in declaration when a project has more than one form? Yes. In this case, you may need to consider whether a variable declared in one module at the class level (local variables are local, anyway) should be available (accessible) to the other modules. Two commonly used access modifiers (modifiers that define the scope) are Public and Private. You can declare variables as follows:

Public Dim YourVariable As Double Private Dim MyVariable As Single

When a variable is declared with an access modifier, you can omit the keyword Dim. Typically, the declarations appear as follows:

Public YourVariable As Double Private MyVariable As Single

A variable declared as Public is accessible to all other modules in the project; a variable declared as Private is recognized only in that class, and so is a variable declared with a Dim statement at the class level. The following two declarations have the same effect:

Dim MyVariable As Integer Private MyVariable As Integer

Variable accessibility will be discussed in more detail in Chapter 11, “Menus and Multiple-Form Applications,” which discusses multiple form projects.

The discussion of declaration of variables so far has focused on their use for elementary data types. Variables can be (and are) also used to reference other types such as enumerations (groupings of constant names, discussed in Chapter 12, “Object Based Programming”) and objects such as controls. In the case of elementary data types, structures, and enumerations, variables are ready to be used after they are declared. In the case of objects, however, it usually takes two steps: one step to declare the variable as the type, and another step to create (instantiate) and associate the object with the variable. As an example, consider the case of the Random object (an object that you can use in place of the Rnd function). To use it, you will first declare a variable of the Random type as follows:

Dim R As Random

In the second step, you will create the Random object with the New keyword, and assign it to the variable R as shown below:

R = New Random()

From that point on, you will be able to use the variable R to generate random numbers that suit your need. (Note that Random is followed by a pair of parentheses.)

Understanding Objects

Technically, Random as a type is similar to an icon in the Toolbox, such as the Text Box icon. The icon in the Toolbox is not yet an object. It is simply a template from which you can create instances of objects. For examples, you can draw many text boxes onto a form, creating many instances of text boxes from the template. The formal term for the template is class; for the instance(s), object. The action of creating an object (instance) from the class is termed instantiation. Use these terminologies to restate the two preceding steps: You first declared the variable R as the Random class; you then created an instance of the Random class and associated it with R.

In many cases, the two steps are combined into one—that is, the object is created and associated with the variable at the same time when the variable is declared. Again, the New keyword is used to create the object. The following statement accomplishes both steps:

Dim R As New Random()

The use of the Random object will be discussed later in this chapter. This discussion of declaration of variables (and the terms, object and class) may not appear very clear to you. This is natural because of the huge body of knowledge in VB that you need to absorb in a short period. You may want to come back and revisit this section a few more times to gain additional familiarity and understanding.

Last change: February 13 2016 18:47:34.
<<PreviousNext>>