|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Inheritance and function issuecondo. 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 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. Show quoteHide quote "Curtis Ransom" <CurtisRan***@discussions.microsoft.com> wrote in message news:95ADE2F0-269C-4832-B828-F46F8457C0F5@microsoft.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
Show quote
Hide quote
"Curtis Ransom" <CurtisRan***@discussions.microsoft.com> wrote in message It can.news:95ADE2F0-269C-4832-B828-F46F8457C0F5@microsoft.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. > RIght now, it returns a object that does not have the specific property Correct - it returns a House object which may be /either/ a Townhome> values that are specific to either a Townhome or Condo. /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.
Converted a solution to VS2005 and it is complaining: Variable 'PF2' is used before it has been assi
Problem with installer made in VS 2003.NET setting datagrid column widths in code? Detect Concurrent Running Instances How do I maintain paragraph numbers into a new word document Run from Server or Workstation FileDownload with WebBrowser Control - disable the Save? write Override methods in Code Editor VS2005 Force ASP.NET shadow directory to update? |
|||||||||||||||||||||||