Starting Out with Objects

Starting Out with Objects

Let's start out by describing our terms. Many beginners have a difficult time sorting out the difference between a class and an object. I hear them using the terms interchangeably. As I wrote in Chapter 1, a class contains the instructions for how to construct an object in the computer's memory. Here's an analogy.

A Class Is Really Only a Blueprint

Think of a class as a blueprint for building a house. A blueprint isn't a house; it's a sheaf of papers with drawings and dimensions that tell a contractor how to build a house. When the contractor follows the blueprints and builds the house, you have an object. An object is a physical manifestation of a class, just like a house is a physical manifestation of a blueprint.

A blueprint might indicate the location for each window and the type of window to use. A class can include the types of controls and their positions on a form, as well as various data types to use. A house is built with the appropriate windows in the right location. A form object is created with the controls displayed as the form class directed. Of course, contractors use the same blueprints to build several houses, as shown in Figure 2-1. Likewise, you can create several objects from the same class. Because the objects follow the same class blueprint, each of the objects will look and function in the same way.

Figure 2-1

A blueprint is like a class; houses built from the blueprints are like objects.

Let's Talk Objects

The best way to introduce a few of the key object-oriented concepts is with an example. Start up Visual Studio .NET and follow these steps:

  1. Create a new Windows Application project. You'll have a blank form.

  2. Drag a button control from the tool palette to the form. If you've worked with Visual Basic 6, this is old hat to you. Keep the default names of the form and the button control.

    note

    There are three ways to add a control to a form. You can double-click the control, you can click once on the control and then once on the form, or you can click the control and then drag it to the form.

  3. Double-click the button control. Double-clicking the control automatically adds the Click event handler to the form's class.

  4. Click the Form1.vb tab and modify the Text property of both the button and the form objects. For the form, change the Text property to "Mirror Image". For the button, change the value to "&Clone Me!" You can tell the form and the button are both objects because you can set their properties and see that both have inherited the know-how to do things such as resize themselves.

    note

    The ampersand (&) used in the button's Text property automatically provides an accelerator key combination for power users. Visual Basic .NET adds an underscore to the character that immediately follows the ampersand when the text is displayed on the control. A user can press Alt plus the accelerator key to simulate clicking the button.

  5. Size the form and button so that they look something like what's shown in Figure 2-2.

    Figure 2-2

    Your form should look like this.

Our Form as an Object

All objects are created as identical copies of their class; they are mirror images. Your form is a perfect example of an object. Once you instantiate an object from your class, however, the object is separate from any other object you instantiate from the class. After your form is created in your project, you can resize it in the Designer, for example. The form itself is responsible for handling the implementation of how it is resized and redrawn. It is born knowing how to do that and everything else a form does, such as displaying its own default menu and dismissing itself when you click its Close button.

A key point you should understand about objects is that they are a combination of data and code. The fundamental advantage of OOP is that the data and the operations that manipulate the data are both contained in the object. An object can be treated as a self-contained unit.

Objects are the building blocks of an object-oriented program, and an object-oriented program is essentially a collection of objects. In the simple program we just created, we have a button object on a form object. When the form appears, it has a color, size, and position on the screen. These characteristics are among what are called its properties. Properties define the state of an object.

The form also knows how to minimize and maximize itself and how to resize itself when you drag its edges with a mouse. In other words, the form has a set of built-in behaviors, and these behaviors are implemented by what are called methods. Methods tell an object how to do things.

Seeing Properties and Methods in the IDE

The IntelliSense feature in the IDE puts everything the form is or can do at your fingertips. For example, when you enter "Form1" in the code editor, Visual Basic .NET knows that this refers to an object because you've defined Form1 as such—an object that inherits from the Windows.Forms.Form class. The IDE displays each legitimate property or method when you enter a dot (.) after Form1. The dot, or scope resolution modifier, separates the object from its methods or properties. The general form is Object.Method or Object.Property. Properties are marked by an icon of a hand holding a card, while methods are indicated by purple flying bricks.

Notice that Form1 was declared with an uppercase "F," but you could have entered form1 with a lowercase "f." Unlike C, C++, or C#, Visual Basic is not case sensitive, but the IDE will automatically correct your typing and make the spelling consistent.

You could create five forms, each of a different size and with different captions, so that each has its own Size and Text properties set to a different value. Each object contains its own properties. Each is a self-contained black box of functionality that contains data (its size and color) and code for how it does things (resizing or redrawing itself, for example).

If you've programmed before in classic Visual Basic, these concepts might be familiar. For example, any time you placed a list box control on a form and changed its name or size, you were modifying its properties. Whenever you added an item to the list box with AddItem, you were calling one of the list box's methods.

So, just to summarize, the key elements of an object are

  • Properties.  A characteristic of a form (or other object), such as its size, color, and position or the font used for displaying text. Properties contain values that are unique to each object. Most visual controls, such as our form, expose properties to define their appearance.

  • Methods.  Something an object knows how to do. A form object, for example, can resize itself, display a menu, or hide itself.

Reading, Writing, Invoking

You communicate with an object programmatically by reading and writing its properties and invoking its methods. If you wanted to read the current Height property of a form and display it in a message box, you would write the following line in the Load event of the form:

Private Sub Form1_Load(ByVal sender As System.Object,      ByVal e As System.EventArgs) Handles MyBase.Load     MessageBox.Show("Form1's height is " & _         Me.Height, Me.Text) End Sub

note

The keyword Me, which we first saw in Chapter 1, might look strange to those of you new to OOP. Me is the equivalent of the this pointer in C++. Me is used to reference the current object, which happens to be a form. The Me keyword is one of the first elements you will have to become familiar with in Visual Basic .NET.

The first call in this code fragment is to the Show method of the MessageBox class, new to Visual Basic .NET. The Show method knows how to display a message box. The first parameter is what will be displayed as the message. Because we want to display the height of the form, we read its Height property as the second parameter of the Show method. The code Me.Height reads the form's Height property. The final parameter of the Show method is the title to show in the message box's caption. The code Me.Text reads the form's Text property. Now when you run the program, the dialog box shown in Figure 2-3 appears.

Figure 2-3

This message box displays the form's height.

If you wanted to change the form's Height property—write to it instead of read it—you simply assign a new value to the property. To change the form's height from 115 pixels to 203 pixels when the user clicks the button, you would write this code:

Protected Sub Button1_Click(ByVal sender As Object, _     ByVal e As System.EventArgs)     Me.Height = 203     MessageBox.Show("Form1's height is " & _         Me.Height, Me.Text) End Sub

When the user clicks the button, the first line of code assigns the value 203 to the form's Height property, invoking the form's Size method. The form immediately adjusts to the new size. The message box displays the height, confirming that the form resized itself, as shown in Figure 2-4. All you did was assign a new property value; the form object already knew how to resize itself through its Size method.

Figure 2-4

The resized form.

The form object automatically knows whether you are reading or writing a value by the position of the object reference relative to the equal sign (=). To assign a value to a property, you use code like the following:

Me.Height = 203

With the object reference on the left side of the equal sign, the object knows you are assigning a value to the property.

To read the value of a property, you use code like the following:

MyVariable = Me.Height

Here the object reference is on the right side of the equal sign, which means you're reading the property's value. As I stressed before, you don't know how the property is stored inside the object. It might be stored in Portuguese, pig Latin, or any language at all. All you know is that you can read an integer from the property or write an integer to set the property. How the object performs these operations is its business. An object can seem very much like the Wizard of Oz when he says, "Pay no attention to the man behind the curtain."

This kind of implementation is one of the productive aspects of working with objects. To use an object you simply read or write its properties and call the methods that it exposes. The object is responsible for doing the real work. An object might be prepackaged like a form, or it might be one you write yourself.



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

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