Testing Polymorphism


Now that you have finished modifying the derived classes, you need to look at the code used to test your changes. The simple form that is used to test the modified Building class and the derived classes is shown in Figure 18.1.

Figure 18.1. A form that is used to test the modified Building class and the derived classes.

graphics/18fig01.jpg

The single text box is named txtOuput , and it has the Multiline property set to True . This text box is used to show the snow removal phone numbers . The two buttons are named btnProperties and btnExit . The code for the test program is presented in Listing 18.4.

Listing 18.4 New Code to Test the Building Class and the Derived Classes
 Option Strict On Public Class frmVirBldg  Inherits System.Windows.Forms.Form  Private Const APARTMENT As Integer = 0  Private Const COMMERCIAL As Integer = 1  Private Const HOME As Integer = 2 ' Windows Form Designer generated code  Private Sub frmBuilding_Load(ByVal sender As System.Object, ByVal e As _                 System.EventArgs) Handles MyBase.Load  End Sub  Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As _               System.EventArgs) Handles btnExit.Click   Me.Dispose()  End Sub  Private Sub btnProperties_Click(ByVal sender As Object, ByVal e As _                System.EventArgs) Handles btnProperties.Click   Dim CurrentBuilding As Building   Dim MyApartment As New Apartment("6 North Ben Street", 550000, 6000, _                    12000, APARTMENT, "555-1000")   Dim MyBuilding As New Commercial("250 Industrial Drive", 320000, 3800, _                    5800, COMMERCIAL, "")   Dim MyHome As New Home("2 Ghent Court", 140000, 1300, 3300, HOME, "")   Dim delta As New Home()   Dim MyInvestments As Building() = New Building() {MyApartment, _                         MyBuilding, MyHome, delta}   delta.SnowRemoval = "555-3333"   For Each CurrentBuilding In MyInvestments    txtOutput.Text &= CurrentBuilding.CallSnowRemoval & vbCrLf   Next   MyApartment.DisplayBuilding()   MyBuilding.DisplayBuilding()   MyHome.DisplayBuilding()   delta.DisplayBuilding()   Console.WriteLine(delta.CallSnowRemoval())  End Sub End Class 

Most of the code in Listing 18.4 is boilerplate code that Visual Basic .NET supplies . The interesting stuff takes place in the btnProperties object's Click() event code, as explained in the next section.

Working with Virtual Classes

The btnProperties object's Click() event code contains the following line:

 Dim CurrentBuilding As Building 

This statement should be a red flag to you. Recall from Chapter 17 that any base class that is implemented by using the MustInherit keyword is a virtual (or abstract) class. You cannot instantiate an object of a virtual class. So why doesn't the preceding Dim statement cause Visual Basic .NET to generate an error message?

Remember that you can instantiate an object only when you use the New keyword. Because the Dim statement does not include the New keyword, you are not instantiating a Building object. Instead, you are defining a reference variable that can be used to point to an object of type Building . Recall that reference variables have an rvalue that is the lvalue of where the object is actually stored in memory. Therefore, the statement does not define an object; rather, it defines a pointer to an object. Until the New keyword is used, there is no class object created by the statement.

Using a For Each Construct for an Array of Objects

The next four lines in the btnProperties Click() event simply instantiate objects of the derived classes. The first three objects are created by using the constructors that have arguments passed to them. The last object ( delta ) is instantiated without any arguments to show how a base class constructor with no arguments is called.

The following code defines MyInvestments to be an array of Building references:

 Dim MyInvestments As Building() = New Building() {MyApartment, _                        MyBuilding, MyHome, delta} 

You have initialized the references of this array to point to MyApartment , MyBuilding , MyHome , and delta . You can now use the CurrentBuilding reference pointer to walk through each object in the MyInvestments array. A For Each loop is used to iterate through a collection of objects. Because MyInvestments is an array of object references, you can use the For Each construct to iterate through the array. These lines cause the CurrentBuilding reference to point to each object in the MyInvestments array:

 For Each CurrentBuilding In MyInvestments   txtOutput.Text &= CurrentBuilding.CallSnowRemoval & vbCrLf Next 

For example, on the first pass through the For Each loop, CurrentBuilding is implicitly set as follows :

 CurrentBuilding = MyInvestment(0) 

From the initializer list for MyInvestments , you can see that when CurrentBuilding refers to MyInvestment(0) , you are actually looking at the MyApartment object. It follows that on the second pass through the For Each loop, CurrentBuilding refers to MyInvestment(1) , or the MyBuilding object. The loop continues until all four objects are displayed in the txtOutput text box.

The remaining lines simply show how you might call the DisplayBuilding() method and the CallSnowRemoval() method. A sample run of the program is shown in Figure 18.2.

Figure 18.2. A sample run of the test code for the modified Building class and the derived classes.

graphics/18fig02.jpg

The text box in Figure 18.2 shows the output associated with the passes through the For Each loop, and the Debug window in the background shows the results of the DisplayBuilding() and CallSnowRemoval() calls.

The important thing to note here is that the same message sent to the various building objects produces different results. In each case, the call to the CallSnowRemoval() method causes a different string to be displayed. In a more realistic setting, the message might activate an automated dial-up machine that would dial the appropriate snow removal service for the apartments and commercial buildings and then call the tenant of each rental home to remind him or her to shovel the walk.

It would be worth your effort to single-step through the test program to get a feel for how Visual Basic .NET processes the code. Trust me, single-stepping through code is a great way to learn how objects are instantiated and how the base and derived classes interact with each other.



Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

Similar book on Amazon

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