home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


This avoids setting up a global variable needlessly. In fact, one of the most important aspects of Visual Basic programming is variable scope. In general, you should restrict variables to the smallest scope possible.

There are three levels of variable scope in Visual Basic, as follows:

  Variables declared in procedures are private to the procedure.
  Variables declared at the form or module level in the form or module’s (General) section using Dim, ReDim, Private, Static, or Type are form- or module-level variables. These variables are available throughout the module.
  Variables declared at the module level in the module’s (General) section using Public are global and are available throughout the project, in all forms and modules. Note that you cannot use Public in procedures.

You can get an overview of the scope of variables in a Visual Basic project in Figure 3.1.


Figure 3.1  Visual Basic’s variable scope schematic.

For more information, see the discussion of variable scope in Chapter 1.


TIP:  If you use the Option Private Module statement in a module or form, all variables in the module or form become private to the module, no matter how they are declared.

Verifying Data Types

You can change a variable’s type with ReDim in Visual Basic, assign objects to variables using Set, and even convert standard variables into arrays. For these and other reasons, Visual Basic has a number of data verification functions, which appear in Table 3.3, and you can use these functions to interrogate objects and determine their types.

Table 3.3 Data verification functions.
Function Does This
IsArray() Returns True if passed an array
IsDate() Returns True if passed a date
IsEmpty() Returns True if passed variable is uninitialized
IsError() Returns True if passed an error value
IsMissing() Returns True if value was not passed for specified parameter in procedure call
IsNull() Returns True if passed NULL
IsNumeric() Returns True if passed a numeric value
IsObject() Returns True if passed an object

Note in particular the IsMissing() function, which many programmers don’t know about; this function tells you if the call to the current procedure included a value for a particular variant. For example, here’s how we check if the call to a subroutine CountFiles() included a value in the optional parameter intMaxFiles:

Sub CountFiles(Optional intMaxFile As Variant)
    If IsMissing(intMaxFile) Then
        'intMaxFiles was not passed
...
    Else
...
    End If
End Sub

Declaring Arrays And Dynamic Arrays

It’s time to start coding that database program. But wait a moment—how are you going to handle the data? It’s just a simple program, so you don’t want to start tangling with the full Visual Basic database techniques. An array would be perfect; how do you set them up again?

You can use Dim (standard arrays), ReDim (dynamic arrays), Static (arrays that don’t change when between calls to the procedure they’re in), Private (arrays private to the form or module they’re declared in), Public (arrays global to the whole program), or Type (for arrays of user-defined types) to dimension arrays.

We’ll start with standard arrays now.

Standard Arrays

You usually use the Dim statement to declare a standard array (note that in Visual Basic, arrays can have up to 60 dimensions):

Dim [WithEvents] varname [([subscripts])] [As [New] type] [, [WithEvents]
varname [([subscripts])] [As [New] type]] ...

The WithEvents keyword is valid only in class modules. This keyword specifies that varname is an object variable used to respond to events triggered by an ActiveX object. The varname identifier is the name of the variable you are declaring.

You use subscripts to declare the array. You set up the subscripts argument this way:

[lower To] upper [, [lower To] upper]

The New keyword enables creation of an object. If you use New when declaring the object variable, a new instance of the object is created on first reference to it.

The type argument specifies the data type of the variable, which may be Byte, Boolean, Integer, Long, Currency, Single, Double, Date, String (for variable-length strings), String * length (for fixed-length strings), Object, Variant, a user-defined type, or an object type. If you don’t specify a type, the default is Variant, which means the variable can act as any type.

Here are a few examples of standard array declarations:

Private Sub Command1_Click()
    Dim Data(30)
    Dim Strings(10) As String
    Dim TwoDArray(20, 40) As Integer
    Dim Bounds(5 To 10, 20 To 100)
    Strings(3) = "Here's a string!"
End Sub


TIP:  You use the Option Base statement at the form- or module-level to set the lower bound for all arrays. The default value is 0, but you can use either of these two statements: Option Base 0 or Option Base 1.

Dynamic Arrays

You can use the Dim statement to declare an array with empty parentheses to declare a dynamic array. Dynamic arrays can be dimensioned or redimensioned as you need them with the ReDim statement (which you must also do the first time you want use a dynamic array). Here’s how you use ReDim:

ReDim [Preserve] varname (subscripts) [As type] [, varname(subscripts)
[As type]] ...

You use the Preserve keyword to preserve the data in an existing array when you change the size of the last dimension. The varname argument holds the name of the array to (re)dimension.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.