19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

356 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

Whenever you double-click on the TextBox icon in the toolbar, a new text box is created.<br />

Although each text box is a separate entity, they all have the same properties <strong>and</strong> methods.<br />

Each text box is said to be an instance of the class TextBox. In some sense, the TextBox<br />

icon in the toolbox is a template for creating text boxes. (When you look at the properties<br />

window for a text box, the dropdown list box at the top of the window says something like<br />

“Text1 TextBox”. “Text1” is the name of the control object <strong>and</strong> “TextBox” is the name of its<br />

class.) You can’t set properties or invoke methods of the TextBox icon, only of the specific<br />

text boxes that it creates. The analogy is often made between the TextBox icon <strong>and</strong> a cookie<br />

cutter. The cookie cutter is used to create cookies that you can eat, but you can’t eat the cookie<br />

cutter.<br />

Object-oriented programs are populated with objects that hold data, have properties,<br />

respond to methods, <strong>and</strong> raise events. (The generation of events will be discussed in the next<br />

section.) Six examples are as follows:<br />

1. In a professor’s program to assign <strong>and</strong> display semester grades, a student object<br />

might hold a single student’s name, social security number, midterm grade, <strong>and</strong><br />

final exam grade. A SemGrade method might calculate the student’s semester<br />

grade. Events might be raised when improper data is passed to the object.<br />

2. In a payroll program, an employee object might hold an employee’s name,<br />

hourly wage, <strong>and</strong> hours worked. A CalculatePay method would tell the object to<br />

calculate the wages for the current pay period.<br />

3. In a checking account program, a check register object might record <strong>and</strong> total<br />

the checks written during a certain month, a deposit slip object might record<br />

<strong>and</strong> total the deposits made during a certain month, <strong>and</strong> an account object might<br />

keep a running total of the balance in the account. The account object would<br />

raise an event to alert the bank when the balance gets too low.<br />

4. In a bookstore inventory program, a textbook object might hold the name,<br />

author, quantity in stock, <strong>and</strong> wholesale price of an individual textbook. A<br />

RetailPrice method might instruct the book object to calculate the selling price<br />

of the textbook. An event could be triggered when the book goes out of stock.<br />

5. In a game program, an airplane object might hold the location of an airplane.<br />

At any time, the program could tell the object to display the airplane at its current<br />

location or to drop a bomb. An event can be triggered each time a bomb<br />

moves so that the program can determine if anything was hit.<br />

6. In a card game, a card object might hold the denomination <strong>and</strong> suit of a specific<br />

card. An IdentifyCard method might return a string such as “Ace of Spades.”<br />

A deck of cards object might consist of an array of card objects. A ShuffleDeck<br />

method might thoroughly shuffle the deck <strong>and</strong> a Shuffling event might indicate<br />

the progress of the shuffle.<br />

The most important object-oriented term is class. A class is a template<br />

from which objects are created. The class specifies the properties <strong>and</strong> methods<br />

that will be common to all objects that are instances of that class. Classes are<br />

formulated in class modules. An object, which is an instance of a class, can be<br />

created in a program with a pair of statements of the form<br />

Private objectName As className ‘In General Declarations section<br />

Set objectName = New className ‘In procedure<br />

In the program, properties of the object is accessed with statements of the form<br />

shown in the following table.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!