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.

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

Methods cmdSortAscending_Click (lines 82–93) and cmdSort-<br />

Descending_Click (lines 96–107) are invoked when the user clicks the Sort<br />

Ascending and Sort Descending but<strong>to</strong>ns, respectively. In method<br />

cmdSortAscending_Click, line 87 passes <strong>to</strong> CDelegateBubbleSort method<br />

SortArray the unsorted mElementArray and a reference <strong>to</strong> method SortAscending.<br />

Keyword AddressOf returns a reference <strong>to</strong> method SortAscending.<br />

<strong>Visual</strong> <strong>Basic</strong> implicitly converts the method reference in<strong>to</strong> a delegate object that contains<br />

the method reference. The second argument of line 87 is equivalent <strong>to</strong><br />

New CDelegateBubbleSort.Compara<strong>to</strong>r(AddressOf SortAscending)<br />

which explicitly creates a CDelegateBubbleSort Compara<strong>to</strong>r delegate object. In<br />

method cmdSortDescending_Click, line 101 passes the unsorted mElementArray<br />

and a reference <strong>to</strong> method SortDescending <strong>to</strong> method SortArray. We continue<br />

<strong>to</strong> use delegates in Chapters 12–14, when we discuss event handling and multithreading.<br />

In Chapter 11, Exception Handling, we discuss how <strong>to</strong> handle problems that might<br />

occur during a program’s execution. The features presented in Chapter 11 enable programmers<br />

<strong>to</strong> write more robust and fault-<strong>to</strong>lerant programs.<br />

SUMMARY<br />

Polymorphism enables us <strong>to</strong> write programs in a general fashion <strong>to</strong> handle a wide variety of existing<br />

and future related classes.<br />

One means of processing objects of many different types is <strong>to</strong> use a Select Case statement <strong>to</strong><br />

perform an appropriate action on each object based on that object’s type.<br />

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

When we override a base class’s method in a derived class, we hide the base class’s implementation<br />

of that method.<br />

With polymorphism, new types of objects not even envisioned when a system is created may be<br />

added without modification <strong>to</strong> the system (other than the new class itself).<br />

Polymorphism allows one method call <strong>to</strong> cause different actions <strong>to</strong> occur, depending on the type<br />

of the object receiving the call. The same message assumes “many forms”—hence, the term polymorphism.<br />

With polymorphism, the programmer can deal in generalities and let the executing program concern<br />

itself with the specifics.<br />

When we apply the MustInherit keyword <strong>to</strong> a class, we cannot create instances of that class.<br />

Instead, we create classes that inherit from the MustInherit class and create instances of those<br />

derived classes.<br />

Any class with a MustOverride method in it must, itself, be declared MustInherit.<br />

Although we cannot instantiate objects of MustInherit base classes, we can declare references<br />

<strong>to</strong> MustInherit base classes. Such references can manipulate instances of the derived classes<br />

polymorphically.<br />

A method that is declared NotOverridable cannot be overridden in a derived class.<br />

Methods that are declared Shared and methods that are declared Private are implicitly NotOverridable.<br />

A class that is declared NotInheritable cannot be a base class (i.e., a class cannot inherit from<br />

a NotInheritable class).

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

Saved successfully!

Ooh no, something went wrong!