Chapter 4

Collecting Objects

“Visual Basic isn’t a real language because it doesn’t have pointers, and without pointers you can’t do data structures.”

Ever heard that one? You might even believe it if you’ve ever taken a computer science course on data structures. People devote entire careers to figuring out new ways of writing linked lists, trees, hash tables, stacks, queues, and other abstract data types—all of which are implemented with pointers. Meanwhile, until recently, Visual Basic was stuck with only one data structure—the humble array—all because, as I noted before, Visual Basic doesn’t do pointers.

There have always been workarounds. In ancient versions of Basic, linked lists and other abstract data structures were sometimes implemented using parallel arrays of data and indexes, with the indexes used to fake pointers. It’s not a pretty sight, and, fortunately, it’s no longer necessary. In modern Visual Basic, there are a lot more options. You can create your own data structures, you can use the predefined ones (arrays and the Collection class), or, as the rest of this chapter explains, you can wrap those predefined data structures in classes that make them behave like more sophisticated data structures.

Visual Basic supports two built-in data structures: arrays and the Collection (with an uppercase C) class. This chapter will describe how to create classes that en­capsulate other data structures. These data structure classes—sometimes called collection (with a lowercase c) classes—come in two forms.

Generic collection classes abstract a particular data structure for storing user variables and objects. The point is the organization of the data and the standard operations for using it. You can store any kind of data you want (as long as it fits in a Variant) in these collection classes. Visual Basic’s Collection class serves as a rough model for some of the generic classes I’ll create. These includeCList, CVector, and CStack classes. The difference between them is how they handle common operations such as adding, removing, searching, indexing, and iterating.

In addition, you can create specific collection classes that provide controlled access to specific types of variables or objects. Visual Basic comes with several such collections: Forms, Printers, and Controls. Some controls provide specific collection classes: Nodes in TreeView, ListImages in ImageList, ListItems in ListView, and so on. Specific collection classes are also common in object models exposed by applications such as Excel. I’ll create a CDrives class that contains a CDrive object for each drive in the system. These classes are carefully designed to control addition and deletion of items so that the collection never contains incompatible items.

One warning before we get started—you’ll find some pretty dense code and text in parts of this chapter. Don’t get discouraged if you have trouble following it the first time. You don’t have to understand every detail of a linked list or iteration class to use one. Some early parts of the chapter are setting the stage for concepts introduced in the last part. It’s OK to skim and come back later.