Click Here!
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


The subscripts term specifies the dimensions of the array using this syntax:

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

The type argument specifies the type of the array. The type 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.

This is one of those topics that is made easier with an example, so here’s an example using dynamic arrays, where we declare an array, dimension it, and then redimension it, like this:

Private Sub Command1_Click()
    Dim DynaStrings() As String
    ReDim DynaStrings(10)
    DynaStrings(1) = "The first string"
    'Need more data space!
    ReDim DynaStrings(100)
    DynaStrings(50) = "The fiftieth string"
End Sub

The Array() Function

You can also use the Array() function to create a new variant holding an array. Here’s how you use Array():

Array(arglist)

The arglist argument is a list of values that are assigned to the elements of the array contained within the variant. Here’s an example that creates an array with the values 0, 1, and 2:

Dim A As Variant
A = Array(0,1,2)


TIP:  If you don’t specify any arguments, the Array() function returns an array of zero length.

We’ll finish this topic with a summary of array-handling techniques.

Array-Handling Techniques Summary

Visual Basic has a number of statements and functions for working with arrays, and they appear in overview in Table 3.4 for easy reference.

Table 3.4 Array-handling techniques.
To Do This Use This
Verify an array IsArray
Create an array Array
Change default lower limit Option Base
Declare and initialize an array Dim, Private, Public, ReDim, Static
Find the limits of an array LBound, UBound
Reinitialize an array Erase, ReDim

Declaring Subroutines

Everyone knows about subroutines: they’re the handy blocks of code that can organize your code into single-purposed sections to make programming easier. Unlike functions, subroutines do not return values; but like functions, you can pass values to subroutines in an argument list.

For reference’s sake, here’s how you declare a subroutine:

[Private | Public | Friend] [Static] Sub name [(arglist)]
...
[statements]
...
[Exit Sub]
...
[statements]
...
End Sub

The Public keyword makes a procedure accessible to all other procedures in all modules and forms. The Private keyword makes a procedure accessible only to other procedures in the module or form in which it is declared. The Friend keyword is used only in class modules and specifies that the procedure is visible throughout the project, but not visible to a controller of an instance of an object. The Static keyword specifies that the procedure’s local variables should be preserved between calls. The name identifier is the name of the procedure. The arglist identifier is a list of variables representing arguments that are passed to the procedure when it is called. You separate multiple variables with commas. The statements identifier is the group of statements to be executed within the procedure.

The arglist identifier has the following syntax:

[Optional] [ByVal | ByRef] [ParamArray] varname [()] [As type]
[= defaultvalue]

In arglist, Optional means that an argument is not required; ByVal means that the argument is passed by value; ByRef means that the argument is passed by reference (ByRef is the default in Visual Basic); ParamArray is used as the last argument in arglist to indicate that the final argument is an array of Variant elements; varname is the name of the variable passed as an argument; type is the data type of the argument; and defaultvalue is any constant or constant expression, which is used as the argument’s default value if you’ve used the Optional keyword.


TIP:  When you use ByVal, you pass a copy of a variable to a procedure; when you use ByRef, you pass a reference to the variable, and if you make changes to that reference, the original variable is changed.

The Exit Sub keywords cause an immediate exit from a Sub procedure. Finally, End Sub ends the procedure definition.

You call a Sub procedure using the procedure name followed by the argument list. Here’s an example of a subroutine:

Sub CountFiles(Optional intMaxFile As Variant)
    If IsMissing(intMaxFile) Then
        'intMaxFiles was not passed
        MsgBox ("Did you forget something?")
    Else
...
    End If
End Sub


TIP:  For an overview of how to comment procedures, see the discussion in Chapter 1.

Declaring Functions

There are two types of procedures in Visual Basic: subroutines and functions. Subroutines can take arguments passed in parentheses but do not return a value; functions do the same but do return values (which can be discarded). A function is a block of code that you call and pass arguments to, and using functions helps break your code up into manageable parts.

For reference’s sake, here’s how you declare a function:

[Private | Public | Friend] [Static] Function name [(arglist)] [As type]
...
[statements]
...
[name = expression]
...
[Exit Function]
...
[statements]
...
End Function


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.