Chapter 18. Object-Oriented Visual Basic 2005


A true object-oriented language supports the following features, all found in Visual Basic 2005: programmer-defined types, encapsulation, specialization, and polymorphism. This chapter will briefly review how these hallmarks of object-oriented programming are implemented in Visual Basic 2005.

Chapter 15 discussed the myriad primitive types built into the Visual Basic 2005 language, such as Integer, Long, and Single. The heart and soul of Visual Basic 2005, however, is the ability to create programmer-defined types.

You specify new types in Visual Basic 2005 by declaring and defining classes. Instances of a class are called objects. The difference between a class and an object is the same as the difference between the concept of a Dog and the particular dog who is sitting at your feet as you read this. You can't play fetch with the definition of a Dog, only with an instance (unless your instance is lazy and would prefer to sleep on the sofa that you just vacuumed thank you very much, and whose only interest is in eating and going out in the snow and then you have to clean his feet and, well, just don't get me started, okay?)

A Dog class describes what dogs are like: they have weight, height, eye color, hair color, disposition, and so forth. They also have actions they can take, such as eat, walk, bark, and sleep. A particular dog (such as my dog, Milo) will have a specific weight (62 pounds), height (22 inches), eye color (black), hair color (yellow), disposition (angelic), and so forth. All dogs implement the same behaviors (though the yap method is optional).

The huge advantage of classes in object-oriented programming is that classes encapsulate the characteristics and capabilities of a type in a single, self-contained, and self-sustaining unit of code. When you want to sort the contents of an instance of a Windows list box control, for example, you tell the list box to sort itself. How it does so is of no concern; that it does so is all you need to know.

An old programming joke asks, how many object-oriented programmers does it take to change a light bulb? Answer: none; you just tell the light bulb to change itself.[*]

[*] How many Microsoft engineers does it take to change a light bulb? None. They changed the standard to dark.

This chapter explains the Visual Basic 2005 language features that are used to specify new classes . The elements of a class its behaviors and properties are known collectively as its class members. Methods are used to define the behaviors of the class, and the state of the class is maintained in member variables (often called fields).



Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net