Almost any application requires that you maintain some data storage in memory. As your application runs, you read and write data in some sort of data structure, and when your application shuts down, it either discards the data structure (and its data) or it writes the data to some persistent storage.
VBA provides two built-in data structures: arrays and collections. Each has its good and bad points, and there are compelling reasons to use each of these structures. (For more information on using arrays and collections, see Chapter 4.) On the other hand, if you’ve previously programmed in other languages or have studied data structures in a college course, you may find the need to use abstract data structures, such as linked lists, binary trees, stacks, and queues, as part of your applications. Although all these structures can be implemented using arrays or collections, neither of those constructs is well suited for linked data structures.
This chapter introduces techniques for using class modules to construct abstract data structures. Amazingly, VBA requires very little code to create these somewhat complex structures. Once you’ve worked through the examples in this chapter, you’ll be able to exploit the power of linked lists, stacks, queues, and binary trees in your own VBA applications. Table 6.1 lists the sample files you’ll find on the accompanying CD-ROM.
Table 6.1: Sample Files
File Name | Description |
DYNAMIC.XLS | Excel 97 file with sample modules and classes |
DYNAMIC.MDB | Access 97 file with sample modules and classes |
LIST.CLS | Linked list class |
LISTITEM.CLS | ListItem class |
LISTTEST.BAS | Test routines for List class |
QUEUE.CLS | Queue class |
QUEUEITEM.CLS | QueueItem class |
QUEUETEST.BAS | Test routines for Queue class |
STACK.CLS | Stack class |
STACKITEM.CLS | StackItem class |
STACKTEST.BAS | Test routines for Stack class |
TREE.CLS | Tree class |
TREEITEM.CLS | TreeItem class |
TREETEST.BAS | Test routines for Tree class |