Page 34 - Excel Progamming Weekend Crash Course
P. 34

d540629 ch01.qxd  9/2/03  9:27 AM  Page 9




                  Session 1 — Microsoft Excel Programming — Why and How                    9


                    Objects and Classes

                       You’ll see the words object and class used in discussions of object-oriented
                       programming, often interchangeably. Technically, they have different mean-
                       ings. A class is a plan or definition, where an object is an implementation of
                       that plan (sometimes called an instance). To use an analogy, the blueprints for
                       a car would be the class, while an actual car built from those plans would be
                       an object. You can create multiple objects from the same class.




               would have to write the code for a menu from scratch for each new program. It would be
               much better if the code for a menu could be written once and then reused as needed in new
               programs.
                  These (and other) factors were the impetus behind the development of a programming
               technique called object-oriented programming, or OOP. With OOP, a program is viewed as a
               collection of related sections, or modules, that have different functions. Some of these
               modules are part of the program’s interface, such as menus, toolbars, and dialog boxes.
               Other modules relate to the data with which the program works, such as (for Excel) work-
               books, worksheets, and cells. Each of these modules is an object; see the “Objects and
               Classes” sidebar for more information on nomenclature.
                  OOP offers numerous advantages compared with older traditional methods of programming.
               These include:

                   Reduced errors. By design, objects are self-contained units that are isolated from
                   each other as much as possible. An object’s interactions with the rest of the program
                   are tightly controlled, and unintended interactions (and the resulting errors) are
                   eliminated.
                   Code reuse. An object — or more accurately, a class — is by its very nature reusable,
                   not only in the same program, but in other programs as well.

                  Excel, as well as the other Office programs, was created using OOP techniques. Under the
               skin, therefore, Excel consists of a large collection of objects that work together to provide the
               program’s functionality. You’ll see how this relates to programming Excel in the next section.


               Components and Automation
               The objects that are part of the Excel application are written so that they are available to
               other programs. In computer talk, the objects are said to be exposed. This is part of the
               Component Object Model (COM) technology that is central to the Windows operating system
               itself as well as to most applications that run on Windows. The term component or COM com-
               ponent is used to refer to objects that are exposed in this manner; therefore, the objects
               that are exposed by Excel are sometimes referred to as components. Note that a single
               component may expose more than one class.
                  How does a programmer make use of exposed components? The answer is another COM
               technology called automation (called OLE — object linking and embedding — automation
               in the past). Automation permits an external program to access and control exposed
   29   30   31   32   33   34   35   36   37   38   39