|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
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:
You can get an overview of the scope of variables in a Visual Basic project in Figure 3.1.
For more information, see the discussion of variable scope in Chapter 1.
Verifying Data TypesYou can change a variables 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.
Note in particular the IsMissing() function, which many programmers dont know about; this function tells you if the call to the current procedure included a value for a particular variant. For example, heres 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 ArraysIts time to start coding that database program. But wait a momenthow are you going to handle the data? Its just a simple program, so you dont 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 dont change when between calls to the procedure theyre in), Private (arrays private to the form or module theyre declared in), Public (arrays global to the whole program), or Type (for arrays of user-defined types) to dimension arrays. Well 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 dont 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
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). Heres 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.
|
|
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. |