Programming Microsoft Visual Basic 6
Chapter 6
Classes
and Objects
As with most OOP features, it’s your responsibility to ensure that the class is well encapsulated. The fact that you’re using a class doesn’t guarantee that the goals of encapsulation are met. In this and the next chapter, I’ll show you how some simple rules—and common sense—help you implement robust classes. A robust class is one that actively protects its internal data from tampering. If an object derived from a class holds valid data and all the operations you perform on that object transform the data only into other valid data (or raise an error if the operation isn’t valid), you can be absolutely sure that the object will always be in a valid state and will never propagate a wrong value to the rest of the program. This is a simple but incredibly powerful concept that lets you considerably streamline the process of debugging your code.
The second goal that every programmer should pursue is code reusability, which you achieve by creating classes that are easily maintained and reused in other applications. This is a key factor in reducing the development time and cost. Classes offer much in this respect, but again they require your cooperation. When you start writing a new class, you should always ask yourself: Is there any chance that this class can be useful in other applications? How can I make this class as independent as possible from the particular software I’m developing right now? In most cases, this means adding a few additional properties or additional arguments to methods, but the effort often pays off nicely. Don’t forget that you can always resort to default values for properties and optional arguments for methods, so in most cases these enhancements won’t really make the code that uses the class more complex than it actually needs to be.
The concept of self-containment is also strictly related to code reuse and encapsulation. If you want to create a class module that’s easily reusable, you absolutely must not allow that class to depend on any entity outside it, such as a global variable. This would break encapsulation (because code elsewhere in the application might change the value of the variable to some invalid data) and above all, it would prevent you from reusing the class elsewhere without also copying the global variable (and its parent BAS module). For the same reason, you should try to make the class independent of general-purpose routines located in another module. In most cases, I prefer to duplicate shorter routines in each class module, if this makes the class easily movable elsewhere.
Polymorphism
Informally, Polymorphism is the ability of different classes to expose similar (or identical) interfaces to the outside. The most evident kind of polymorphism in Visual Basic is forms and controls. TextBox and PictureBox controls are completely different objects, but they have some properties and methods in common, such as Left, BackColor, and Move. This similarity simplifies your job as a programmer because you don’t have to remember hundreds of different names and syntax formats. More important, it lets you manage a group of controls using a single variable (typed as Control, Variant, or Object) and create generic procedures that act on all the controls on a form and therefore noticeably reduce the amount of code you have to write.
next page....
|