Inheritance and function issue

G

Guest

I have a base class, call it house, and two inherited classes: townhome and
condo.
I would like to write a function that could return either a townhome or a
condo. Here is how I am trying to implement it:
---------------------------------------------------------------------
Public MustInherit Class House
......
End Class

Public Class Townhome : Inherits House
......
End Class

Public Class Condo : Inherits House
......
End Class


Public Function GetAvailableHome(ByVal address as string) as House
......
End Function
---------------------------------------------------------------------
Now, GetAvailableHome needs to be able to return either a Townhome or a
Condo. RIght now, it returns a object that does not have the specific
property values that are specific to either a Townhome or Condo. The
returned object is the correct type, but all the inherited class specific
information is gone. Does anyone have any ideas? Is what I am trying to do
even possible in VB.NET. Thanks
 
M

Marina Levit [MVP]

You get back a House, so once you want to access either Townhome or Condo
properties, you need to cast it to the corresponding class. Since
GetAvailableHome returns just a House, there is no way for the compiler to
know which type was actually returned.

If you know in advance which type you are getting, then it may be better to
have a wrapper around GetAvailableHome that is specific to townhouses and
condos, so you don't need to do the casts. I am assuming you know in
advance, otherwise you wouldn't be trying to access condo properties if you
didn't know that the function would return a condo.
 
P

Phill W.

Curtis Ransom said:
I have a base class, call it house, and two inherited classes: townhome
and
condo.
I would like to write a function that could return either a townhome or a
condo.
. . .
Public Function GetAvailableHome(ByVal address as string) as House
.....
End Function
Now, GetAvailableHome needs to be able to return either a Townhome or a
Condo.

It can.
RIght now, it returns a object that does not have the specific property
values that are specific to either a Townhome or Condo.

Correct - it returns a House object which may be /either/ a Townhome
/or/ a Condo. The trick is being able to convert the returned object of
your "base" class into the correct /derived/ Type so that you can work
with it. This is called "down-casting".

Dim newHouse As House _
= GetAvailableHome("SomeAddress")
' newHouse is a House, so can only do House things,
' BUT a House variable can hold a Townhome or a Condo, since both
of these are a "kind of" House - an object of either [derived] Type will
"fit into" a variable of the base Type.

So now you ask the returned object what Type is actually is ...

If TypeOf newHouse Is Townhome Then
Dim newTownhome As Townhome _
= DirectCast( newHouse, Townhome )

newTownhome. ' whatever ...

ElseIf TypeOf newHouse Is Condo Then
Dim newCondo As Condo _
= DirectCast( newHouse, Condo )

newCondo. ' whatever ...

End If

Of course, House /itself/ might have some generic methods or
properties that you could via the House Type, as in

Class House
. . .
Public Property NumberOfDoors() As Integer
End Property
. . .
End Class

then

Dim newHouse As House _
= GetAvailableHome("SomeAddress")

If newHouse.NumberOfDoors < 1 Then
Throw New ForgetfulBuilderException( newHouse ) ' :cool:
End If

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top