Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Using Collections and Object Arrays

In earlier lessons you learned about control arrays that you can declare. By declaring an array of five Option Button controls that all have the same name, you can set property values for one, and all the others gain the same properties. Your application will distinguish between the controls by the control array subscript.

A collection is a set of all objects of the same datatype. In addition to the control arrays, you can work with collections. A collection differs from an array because your application may contain three command button arrays but only one Controls collection. The Controls collection refers to every control used in your application.

Table 22.2 describes common Visual Basic collections.

Table 22.2. Some of the collections you can manage.

Collection Description
Controls All controls within your application
Forms All forms within your application
Printers All printers connected to your system

The collections support several methods that you can use to manage the collection.

Table 22.3 lists some of those methods.

Table 22.3. Some methods you can apply to collections.

Method Description
Add Adds items to collections
Count Returns the number of items in a collection
Remove Deletes items from a collection
Item References a collection element

Suppose that you want to display all controls on the form, even some that might be hidden from other procedures that executed previously. Although you could set each control's Visible property to True, the following loop makes for an easier display of the controls:

For intCtr = 0 to Controls.Count-1
   Controls(intCtr).Visible = True
Next intCtr

The For Each statement makes the loop even simpler. The zero-based collection subscript requires that you loop through the Count-1 subscript, which is a little confusing. Substitute For Each to clarify things and to let Visual Basic take care of the subscripting like this:

Dim ctlControl As Control
For Each ctlControl In Controls
   ctlControl.Visible = True
Next ctlControl

Notice that you must declare a control variable so that the For Each statement has a place to load each control in the collection.

Suppose that you add forms to that same application and you want to make all controls visible on all the forms. The Forms collection makes such a task simple if you use the following nested loop:

Dim ctlControl As Control
Dim frmMyForms As Form
For Each frmMyForms In Forms
   For Each ctlControl In Controls
      ctlControls.Visible = True
   Next ctlControl
Next frmMyForms

If you want to create your own collections, you'll be able to work with them just as you work with the supplied collections. You'll have to declare and manage the collection yourself, but after you build a collection, you can operate on all the collection items more easily than if they were separate or part of a control array.

Given that the Collection keyword is itself a defined object, you can declare a collection like this:

Public colNewCollect As New Collection

If you don't use Dim, but use either Private or Public to declare collections, declare the collections in the general section of a module so that the Public or Private keyword determines the scope (either project- or module-level availability).

If you use Dim and declare a new collection inside a procedure, only that procedure has access to the collection. Often such a local collection is wanted, but be aware that other procedures cannot use the collection.

After you define the collection in the general section, you can create the collection's specific instances. Listing 22.1 declares collection members and shows you how to use the methods to add and manage the collection.

Example 22.1. Creating and managing a collection.

 1: Dim colPeople As New Collection
 2: Dim intCtr As Integer
 3: Dim m As Integer    'MsgBox() return (not used)
 4:
 5: colPeople.Add "George"
 6: colPeople.Add "Sandra"
 7: colPeople.Add "William"
 8: colPeople.Add "Sue"
 9: colPeople.Add "Terry"
10:
11:  ' Print the collection
12: For intCtr = 1 To colPeople.Count
13: m = MsgBox("The next name is " & colPeople(intCtr))
14: Next intCtr
15:
16:  ' Add another person if you wish
17:  ' As you can see, you don't need to
18:  ' concern yourself with running past a
19:  ' maximum subscript value as you
20:  ' would with arrays.
21: colPeople.Add "Kay"
22: 
23:  ' The following should display 6 people
24: m = MsgBox("There are " & Str(colPeople.Count) & _
25: " in the collection.")

Here is the output from this code:

The next name is George
The next name is Sandra
The next name is William
The next name is Sue
The next name is Terry
The next name is Kay
There are 6 in the collection.

The previous discussion shows how you can use the Add method to add new items to the collection. You don't have to worry about a maximum subscript. The problem, however, is that with Add's default method format, you cannot add new collection items except to the end of the collection. In addition, you cannot remove specific items, except for the final collection item, from the collection with Remove.

A named argument is an argument known by its name and not by its specific position within an argument list.

Add supports a named argument, Before, that lets you insert new items into a collection before an existing item. In effect, Visual Basic shifts all the subsequent items down in the list. If you want to add a new name to the beginning of the People collection, code the following:

People.Add "Robert", Before:=1

The collection now looks like this:

Robert
George
Sandra
William
Sue
Terry
Kay

If you want to remove the third name, you can do so like this:

People.Remove 3   ' Deletes the 3rd item

Share ThisShare This

Informit Network