The Building Base ClassThis section shows the code for the Building base class. Based on the discussion to this point in the chapter, the code is presented in Listing 17.1. (Listing 17.1 leaves out a few things from Table 17.1, just to make the code a little more manageable. You might want to add them after you've read the chapter.) Listing 17.1 Code for the Building Base ClassOption Explicit On Option Strict On Public Class Building ' This is the base class, from which we derive the ' apartment, commercial, and home classes. ' =============== Symbolic Constants ================ Private Const APARTMENT As Integer = 0 Private Const COMMERCIAL As Integer = 1 Private Const HOME As Integer = 2 ' ================== Data Members ================== Private mAddress As String ' Street address Private mPrice As Double ' Purchase price Private mMonthlyPayment As Double ' Monthly payment Private mTaxes As Double ' Annual taxes Private mType As Integer ' Building type ' ==================== Properties ====================== Public Property Address() ' Address Property Get Return mAddress End Get Set(ByVal Value) mAddress = Value End Set End Property Public Property PurchasePrice() ' Purchase Price Property Get Return mPrice End Get Set(ByVal Value) mPrice = Value End Set End Property Public Property MonthlyPayment() ' Monthly Payment Property Get Return mMonthlyPayment End Get Set(ByVal Value) mMonthlyPayment = Value End Set End Property Public Property Taxes() ' Property Taxes Property Get Return mTaxes End Get Set(ByVal Value) mTaxes = Value End Set End Property Public Property BuildingType() ' Building Type Property Get Return mType End Get Set(ByVal Value) If Value < APARTMENT OrElse Value > HOME Then mType = -1 ' an error Else mType = Value End If End Set End Property ' ========================== Methods ======================== Protected Sub DisplayBaseInfo() Dim BldType As String ' Purpose: To display the base member data Console.WriteLine("Address: " & mAddress) Console.WriteLine("Purchase Price: " & FormatMoney(mPrice)) Console.WriteLine("Monthly Payment: " & FormatMoney(mMonthlyPayment)) Console.WriteLine("Property Taxes: " & FormatMoney(mTaxes)) Select Case mType Case APARTMENT BldType = "Apartment" Case COMMERCIAL BldType = "Commercial" Case HOME BldType = "Home" End Select Console.WriteLine("Building Type: " & BldType) End Sub ' =========================== Helpers ========================= Protected Function FormatMoney(ByVal num As Double) As String ' Purpose: To format a dollar value ' ' Argument list: ' num A double that is the dollar value to format ' ' Return value: ' string A format dollar value string Return Format(num, "$##########.00") End Function End Class The code in Listing 17.1 begins by defining several symbolic constants for the three types of investment buildings . Next it defines five Private member variables of the base class. Each of these five definitions is followed by a Public property access method. This means that the member variables may be changed, but only through the class interface provided by the property access methods. How could you provide data to a class that is not accessible outside its own class? Simple. You declare the data with the Private access specifier , but you do not provide a Public property access method for the data members. If you do this, even a derived class cannot have access to the Private members of the base class. This is the first line of the Address() property access method: Public Property Address() ' Address Property Because the property access method is Public , the Address() property method can be used to change the value of mAddress . What would happen if you changed the Public keyword to Private ? That would limit the property access method to the Building class itself. Nothing outside the class could then use it. You would not be able to change the mAddress member of the Building class from outside the class because mAddress and its Address() property access method would both be Private . Even derived classes would not have access to mAddress in such a case. Listing 17.1 provides Public property access methods for all member variables in the Building class. It is important to remember that these Private data members can be changed, but only after an object of the class has been instantiated . Even after the object has been created, the class member data can be changed only through the class interface you provide via the Public property access methods. The Protected Access SpecifierTwo methods are provided in the Building class. The first, DisplayBaseInfo() , is used to display the Building class member values for the current object. You use the Console.WriteLine() method to display the values in the Debug window. The second method, FormatMoney() , is used to display monetary values, each with a dollar sign and two decimal places. Programmer's Tip
Note that the DisplayBaseInfo() and FormatMoney() methods use the Protected keyword. Protected is another type of access specifier. However, Protected indicates that the programming element (for example, a data item, a method definition) is accessible from within its own class or a derived class. When you use the Protected access specifier, the data and methods are usable within their own class or a derived class. If you used the Private access specifier for the DisplayBaseInfo() and FormatMoney() methods, objects of the derived classes would not have access to those methods. Because you want the derived classes to be able to take advantage of these two methods, you need to use the Protected access specifier. |