Objectives of the Week
Collections
The Collection Class is used to hold a list of objects. It is similar to an array as you reference each element of the collection using a subscript. Arrays must contain the same type of object, but Collections may contain different types of objects. Arrays are somewhat tedious to manage where the collection uses a standard set of methods to maintain the list. In most cases, a collection can be used anyplace an array is used and the coding will be simpler to support. Although each element is addressed using a subscript, the value of the subscript may be a number, as in an array, or it can be a key value of any type.
The Collection Methods and Properties are self explanitory:
![]() From the Object Browser |
Add | object.Add Object, Key | Adds the Object to the Collection with the Index Value of Key |
Remove | object.Remove Key | Removes the Object from the Collection | |
Item | object.Item(Key) | Reference to the Object | |
Count | object.Count | The number of elements in the Collection |
This code snippet gives you an idea on how you can use collections in your applications:
Option Explicit Dim myCollection As New Collection Dim intInteger As Integer Dim strString As String Dim cTextBox As Control Private Sub Form_Load() intInteger = 100 strString = "String" Set cTextBox = Me.txtBox cTextBox.Text = "TextBox String" myCollection.Add intInteger, "KeyInteger" myCollection.Add strString, "KeyString" myCollection.Add cTextBox, "KeyTextBox" Debug.Print myCollection.Item(1) Debug.Print myCollection.Item(2) Debug.Print myCollection.Item(3) Debug.Print myCollection.Item("KeyInteger") Debug.Print myCollection.Item("KeyString") Debug.Print myCollection.Item("KeyTextBox").Visible Debug.Print myCollection.Count myCollection.Remove "KeyString" Debug.Print myCollection.Count End Sub |
Results:
100 Note that the index can be a number, or the key value in the form of a string can be used to access the contents. Also note that with the TextBox object stored in the collection, the Control Properties can be referenced usning the Collection Item. Collections have many advantages over arrays. |
Visual Basic uses the Collection Class to extensively.
The For - Next Loop is great for array processing as arrays are fixed in size, and often you must process each element in the Array. For Collections, the For - Each Loop is appropriate.
Here are a couple of Examples
To close all forms in a project: Dim
aForm as Form |
To disable all Controls on a Form: Dim
aControl as Control |
To clear all the TexBoxes on a Form: Dim
aControl as Control |
Note the Naming Conventions:
The Collection is always Plural, and the Member is always singular, so a Widget is a
Member of the Widgets Collection.
Your use of collections may be simple, as in the first examples, but it is easy to implement your own set of Collection Classes. To do this, you write two classes. The first is the Collection Class and the second is the Member Class. Each class type has its own set of respoinsibilities, and after you understand the model, it is easy to apply the standard.
The Collection Class must implement the Standard Interface Methods and Properties (Add, Remove, Item and Count)
The Member Class must implement the Get and Let property procedures for each exposed class property, and the methods required to support the class.
When you write your own Collection Class, in order to support the For Each processing, you must create an additional Public Property. This Property MUST be called NewEnum and MUST have the Procedure ID set to -4. From the Collection Class Module, select Tools | Procedure Addributes, and click on the Advanced button.
![]()
The Property Procedure is coded as follows: Public Function NewEnum() As IUnknown ColClass is your Collection Class name. |
![]() |
Build the Example
This example implements a Person Collection Class.
Step by Step through the Example
In addition to the mechanics of Collection Class implementation, concentrate on the separation of the responsibilities of all components.
The Main Form uses the Display Form to show the selected information. The Display Form knows nothing of the Class Modules. The Main Form requests the services of the Collection Class to Add, Remove and Retrieve the requested information. Main knows nothing of the details of the Member Class. There must be an awareness on the part of the developer, the the specifics of how Main gets access to that information is managed between the Collection Class and the Member Class.
Lab 1 -From the Text - Page 375, VB Mail Order
Step by Step through the First Lab
Let me know when you are done and I will check you off as Complete
Lab 2 - From the Text - Page 376, VB Auto Center
You are on your own.
Reading Assignment
Programming Assignment
As Always: The assignment is due before the start of class next week
A Collection of Pets
Page 374, Problem 9.2
This program should implement a single form that has Add and Display capabilities.
Build a Collection Class (cPets) and the Associated Member Class (cPet).For the Add:
- Collect the Member Class information from the screen
- Create a new instance of the Member Class and store the screen input
- Add this class instance to the Collection
For the Display:
- You MUST use the For Each construct to list each pet in the collection
- This will mean a Message Box for each element in the collection