Inheritance/Casting question in vb.net

  • Thread starter Thread starter Sam Kuehn
  • Start date Start date
S

Sam Kuehn

What is the correct way to "widen" the interface of an
object? Let me explain (this is not a real example but
should illustrate my point). Say you have a person
object with some properties. And you also have a
customer object that inherits from the person object and
adds some properties. I know that you cannot directly
cast from a person to a customer. But, I am in a
situation where I need to do exactly that. I could go
and copy all of the properties of the person object into
the customer object but that doesn't seem like a very
good solution. Any ideas?
 
Sam Kuehn said:
What is the correct way to "widen" the interface of an
object? Let me explain (this is not a real example but
should illustrate my point). Say you have a person
object with some properties. And you also have a
customer object that inherits from the person object and
adds some properties. I know that you cannot directly
cast from a person to a customer. But, I am in a
situation where I need to do exactly that.

But how would you fill the properties customers have but persons don't?
 
One way you could do it would be to put a Shared method on the Customer
class which takes a Person object as its argument and returns back a
Customer object for you. You would still be copying over properties, but it
would be encapsulated at least, and you could treat it in a more OO way.
So something like:
Class Customer
Public Shared Function Widen(Who As Person) As Customer
Dim c As New Customer

... copy over props here ...
c.myprop = Who.myprop
... end copying props ...

Return c
End Function
End Class

Then you could call it easily enough,

Dim cust As Customer = Customer.FromPerson(myPerson)

Hope this may help.
 
-----Original Message-----


But how would you fill the properties customers have but persons don't?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


.
Obviously, you cannot fill in the properties of a
customer that a person doesn't have. Most of those
properties are going to be calculated properties. Let me
explain my exact situation and it may make more sense. I
have a stored procedure that returns some numeric
statistics on a book of business (insurance). I have a
class representing these values. Since I have a common
business object that hydrates the BookStats object I can
only have the same number of properties as I have columns
returned from the stored proc. So basically I have a
stored proc that fills in the properties of the
BookStats. Now what I need to do is extend this object.
I need to provide some other stats that should not be
returned by the stored proc they are calculated values
that I would like to keep in my BLL. So I was hoping
that I could have an ExtendedBookStats class that would
take a BookStats class and its current values and give me
the extra properties and presumably methods that I need.
Hope this makes sense.
 
-----Original Message-----
One way you could do it would be to put a Shared method on the Customer
class which takes a Person object as its argument and returns back a
Customer object for you. You would still be copying over properties, but it
would be encapsulated at least, and you could treat it in a more OO way.
So something like:
Class Customer
Public Shared Function Widen(Who As Person) As Customer
Dim c As New Customer

... copy over props here ...
c.myprop = Who.myprop
... end copying props ...

Return c
End Function
End Class

Then you could call it easily enough,

Dim cust As Customer = Customer.FromPerson(myPerson)

Hope this may help.





.
Looks like an acceptable solution. Thank you.
 
Sam,
In addition to the other comments.

I would consider making the Customer object contain a reference to the
Person object, then delegate all the "Person" properties to the underlying
Person object.

Alternatively rather then having Customer inherit from Person, I would
consider using the Role Pattern and make Customer a Role that Person can
have. This allows Person to change its Role (to Customer) without needed to
copy the Person attributes.

' quick sample of the Role Pattern:

Public MustInherit Class PersonRole

Private Class NullRole
Inherits PersonRole

' default behavior & attributes for a Person without a specific
role.

End Class

Public Shared ReadOnly Null As PersonRole = New NullRole

' behavior & attributes common to roles

End Class

Public Class Person

Private m_role As PersonRole

Public Sub New()
m_role = PersonRole.Null
End Sub

Public Property Role() As PersonRole
Get
Return m_role
End Get
Set(ByVal value As PersonRole)
If value Is Nothing Then Throw New
ArgumentNullException("Role")
m_role = value
End Set
End Property

' behavior & attributes specific to a Person

End Class

Public Class CustomerRole
Inherits PersonRole

' behavior & attributes specific to a Customer

End Class

Person.Role could be a collection if a Person supported multiple
simultaneous roles.

Hope this helps
Jay
 

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

Back
Top