Programming Microsoft Visual Basic 6
Chapter 6
Classes
and Objects
Most important, objects are the base on which almost every feature of Visual Basic is implemented. For example, without objects you can’t do serious database programming, you can’t deliver Internet applications, and you can’t write components for COM, DCOM, or MTS. In short, you can do little or nothing without a firm grasp on what objects are and how you can take advantage of them.
If you’re absolutely new to object-oriented programming, this could be the most difficult chapter of the entire book for you to grasp. To understand how objects can help you write better programs in less time, you must be ready for a conceptual leap, not unlike the leap that many programmers had to take when switching from pure procedural MS-DOS languages such as QuickBasic to newer and more sophisticated event-driven programming environments such as Visual Basic. But once you grasp the basic concepts of OOP, you’ll probably agree that objects are the most exciting thing to happen to Visual Basic since its first version. When you dive into object-oriented programming, you’ll soon find yourself devising new, concise, and elegant solutions to old problems, often in less time and with less code. But I don’t want to sound intimidating. As a Visual Basic programmer, you’ve already learned to master many advanced programming techniques concerned with, for example, events, database programming, and user interface issues. OOP isn’t more difficult, it’s merely different. And it’s certainly a lot of fun.
If you’ve ever read books or articles about OOP, you surely found dozens of different definitions for the term object. Most of the definitions are correct and confusing at the same time. The definition I like most is this one:
An object is an entity that embeds both data and the code that deals with it.
Let’s see what this means in practice.
The Basic Concepts
I have noticed that many programmers exposed for the first time to OOP tend to confuse classes and objects, so a very short explanation is in order. A class is a portion of the program (a source code file, in Visual Basic) that defines the properties, methods, and events—in a word, behavior—of one or more objects that will be created during execution. An object is an entity created at run time, which requires memory and possibly other system resources, and is then destroyed when it’s no longer needed or when the application ends. In a sense, classes are design time–only entities, while objects are run time–only entities.
next page....
|