Object component practicalities

Object components are built using a special Visual Basic module type called a class module. The class module can contain properties, methods, and events and can consume properties, methods, and events from other classes (described later). Our example diagrams so far have been high level; that is, only the overall functionality of the object has been shown. In reality, an object component will usually consist of contained classes—each with properties, methods, and events. Figure 15-4 shows a more detailed example of how applications might interact with object components.

Figure 15-4 Classes within object components

For the programmer, using object components couldn’t be simpler. An object component is written in ordinary Visual Basic code. To use an object, the programmer simply has to declare the object, instantiate it, and then call its methods and properties. Two additional and powerful features have been added to Visual Basic 5 that greatly increase the power of object components: the Implements statement and the Events capability.

The Implements statement allows you to build objects (class objects) and implement features from another class (base class). You can then handle a particular procedure in the new derived class or let the base class handle the procedure. Figure 15-5 on the following page shows an imaginary example of how Implements works in principle. The exact coding methods are not shown here because they are covered fully in the online documentation that comes with Visual Basic 5. The example in Figure 15-5 is of an airplane autopilot system.

Figure 15-5 shows a base Autopilot class that has TakeOff and BankLeft methods. Because different airplanes require different procedures to take off, the base Autopilot class cannot cater to individual take-off procedures, so instead it contains only a procedure declaration for this function. The BankLeft actions, however, are pretty much the same for all airplanes, so the Autopilot base class can perform the required procedures.

Figure 15-5 Example using the Implements statement

There are two types or classes of airplane in this example: a B737 and a Cessna. Both classes implement the autopilot functionality and therefore must also include procedures for the functions that are provided in the Autopilot base class. In the TakeOff procedure, both the Cessna and B737 classes have their own specific implementations. The BankLeft procedures, however, simply pass straight through to the BankLeft procedure in the Autopilot base class. Now let’s say that the BankLeft procedure on the B737 changes so that B737 planes are limited to a bank angle of 25 degrees; in this case, you would simply replace the code in the B737 class BankLeft procedure so that it performs the required action.

Visual Basic 4 users might have noticed something interesting here: the Cessna and B737 classes have not instantiated the Autopilot class. This is because the instancing options for classes have changed in Visual Basic 5. It is now possible to create a class that is global within the application without having to declare or instantiate it. Here are the new instancing settings:

To set the Instancing property, you must first set the Project Type to ActiveX EXE. The ActiveX DLL option doesn’t allow any of the SingleUse options, and the ActiveX Control option allows only Private or PublicNotCreatable. For more information on the use of the Instancing property, see Chapter 2.

The new Events capability in Visual Basic 5 is the second useful and powerful new feature that is available to object classes. Essentially, it allows your object class to trigger an event that can be detected by clients of the object class. The “Introducing the progress form” section on page 641 gives an example of how events are used.