Chapter 8. Classes and Objects

   

Chapter 5 discusses the intrinsic types, such as Integer, Long, and Single, that are built into the VB.NET language. As you may recall, these simple types allow you to hold and manipulate numeric values and strings. The true power of VB.NET, however, lies in its capacity to let the programmer define new types to suit particular problems. It is this ability to create new types that characterizes an object-oriented language. You specify new types in VB.NET by declaring and defining classes .

Particular 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.

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. He is capable of all the actions ” methods , in programming parlance ”of any dog (though if you knew him you might imagine that eating is the only method he implements).

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 unit. Suppose, for instance, you want to sort the contents of an instance of a Windows list box control. The list box control is defined as a class. One of the properties of that class is that it knows how to sort itself. Sorting is encapsulated within the class, and the details of how the list box sorts itself are not made visible to other classes. If you want a list box sorted, you just tell the list box to sort itself, and it takes care of the details.

So, you simply write a method that tells the list box to sort itself ”and that's what happens. How it sorts is of no concern; that it does so is all you need to know.

As noted in Chapter 3, this is called encapsulation, which along with polymorphism and inheritance, is one of three cardinal principles of object-oriented programming. Polymorphism and inheritance are discussed in Chapter 11.

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. [1] This chapter explains the VB.NET language features that are used to specify new classes. The elements of a class ”its behaviors and its state ”are known collectively as its class members .

[1] Alternate answer: "None, Microsoft has changed the standard to darkness ."

Class behavior is created by writing methods (sometimes called member functions). A method is a small routine that every object of the class can execute. For example, a Dog class might have a bark method; a list box class might have a sort method.

Class state is maintained by fields (sometimes called member variables ). Fields can be primitive types (e.g., an Integer to hold the age of the dog, a set of strings to hold the contents of the list box), or fields can be objects of other classes (e.g., an Employee class may have a field of type Address).

Finally, classes may also have properties, which act like methods to the creator of the class, but look like fields to clients of the class. A client is any object that interacts with instances of the class.

   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

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