Programming Microsoft Visual Basic 6
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.