30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

396 Object-Oriented <strong>Program</strong>ming: Polymorphism Chapter 10<br />

3. Referring <strong>to</strong> a derived-class object with a base-class reference is safe, because the<br />

derived-class object is an object of its base class. <strong>How</strong>ever, this reference can refer<br />

only <strong>to</strong> base-class members. If this code refers <strong>to</strong> derived-class-only members<br />

through the base-class reference, the compiler reports an error.<br />

4. Referring <strong>to</strong> a base-class object with a derived-class reference generates a compiler<br />

error (when Option Strict is On). To avoid this error, the derived-class reference<br />

first must be cast <strong>to</strong> a base-class reference. In this cast, the derived-class<br />

reference must reference a derived-class object, or <strong>Visual</strong> <strong>Basic</strong> generates an InvalidCastException.<br />

Common <strong>Program</strong>ming Error 10.4<br />

After assigning a derived-class object <strong>to</strong> a base-class reference, attempting <strong>to</strong> reference derived-class-only<br />

members with the base-class reference is a syntax error. 10.4<br />

Common <strong>Program</strong>ming Error 10.5<br />

Treating a base-class object as a derived-class object can cause errors. 10.5<br />

Though it is convenient <strong>to</strong> treat derived-class objects as base-class objects by manipulating<br />

derived-class objects with base-class references, doing so can cause significant problems.<br />

For example, in a payroll system we need <strong>to</strong> be able <strong>to</strong> walk through an array of<br />

employees and calculate the weekly pay for each person. Intuition suggests that using baseclass<br />

references would enable the program <strong>to</strong> call only the base-class payroll calculation<br />

routine (if there is such a routine in the base class). We need a way <strong>to</strong> invoke the proper<br />

payroll calculation routine for each object, whether it is a base-class object or a derivedclass<br />

object, and <strong>to</strong> do this simply by using the base-class reference. We learn how <strong>to</strong> create<br />

classes that include this behavior as we introduce polymorphism throughout this chapter.<br />

10.3 Type Fields and Select Case Statements<br />

One way <strong>to</strong> determine the type of an object that is incorporated in a larger program is <strong>to</strong> use<br />

a Select Case statement. This allows us <strong>to</strong> distinguish among object types, then invoke<br />

an appropriate action for a particular object. For example, in a hierarchy of shapes in which<br />

each shape object has an mShapeType instance variable, a Select Case structure could<br />

employ the object’s mShapeType <strong>to</strong> determine which Print method <strong>to</strong> call.<br />

<strong>How</strong>ever, using Select-Case logic exposes programs <strong>to</strong> a variety of potential problems.<br />

For example, the programmer might forget <strong>to</strong> include a type test when one is warranted<br />

or the programmer might forget <strong>to</strong> test all possible cases in a Select Case. When<br />

modifying a Select-Case-based system by adding new types, the programmer might<br />

forget <strong>to</strong> insert the new cases in all relevant Select-Case statements. Every addition or<br />

deletion of a class requires the modification of every Select-Case statement in the<br />

system; tracking these statements down can be time-consuming and error-prone.<br />

Software Engineering Observation 10.2<br />

Polymorphic programming can eliminate the need for unnecessary Select-Case logic. By<br />

using <strong>Visual</strong> <strong>Basic</strong>’s polymorphism mechanism <strong>to</strong> perform the equivalent logic, programmers<br />

can avoid the kinds of errors typically associated with Select-Case logic. 10.0

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

Saved successfully!

Ooh no, something went wrong!