473,386 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Inheritance and function issue

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
Jan 27 '06 #1
2 917
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.

"Curtis Ransom" <Cu**********@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
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

Jan 27 '06 #2
"Curtis Ransom" <Cu**********@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
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 ) ' 8-)
End If

HTH,
Phill W.
Jan 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes...
4
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by...
8
by: Shawn Casey | last post by:
Consider the following code: interface IBase { virtual void BaseFunction() = 0; }; interface IDerived : public IBase { virtual void DerivedFunction() = 0;
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
14
by: Mark O'Flynn | last post by:
I would like some advice regarding implementing inheritance using vb.net. I have an application written in vb6 that I am rewritting from the ground up in vb.net to take full advantage of .net,...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.