473,321 Members | 1,669 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,321 software developers and data experts.

Inheritance/Casting question in vb.net

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?
Nov 21 '05 #1
8 2267
"Sam Kuehn" <sa******@hotmail.com> schrieb:
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?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #2
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.
"Sam Kuehn" <sa******@hotmail.com> wrote in message
news:09****************************@phx.gbl...
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?

Nov 21 '05 #3
-----Original Message-----
"Sam Kuehn" <sa******@hotmail.com> schrieb:
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?
--
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.
Nov 21 '05 #4
-----Original Message-----
One way you could do it would be to put a Shared method on the Customerclass which takes a Person object as its argument and returns back aCustomer object for you. You would still be copying over properties, but itwould 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.
"Sam Kuehn" <sa******@hotmail.com> wrote in message
news:09****************************@phx.gbl...
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?

.

Looks like an acceptable solution. Thank you.
Nov 21 '05 #5
-----Original Message-----
"Sam Kuehn" <sa******@hotmail.com> schrieb:
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?
--
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.
Nov 21 '05 #6
-----Original Message-----
One way you could do it would be to put a Shared method on the Customerclass which takes a Person object as its argument and returns back aCustomer object for you. You would still be copying over properties, but itwould 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.
"Sam Kuehn" <sa******@hotmail.com> wrote in message
news:09****************************@phx.gbl...
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?

.

Looks like an acceptable solution. Thank you.
Nov 21 '05 #7
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

"Sam Kuehn" <sa******@hotmail.com> wrote in message
news:09****************************@phx.gbl...
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?

Nov 21 '05 #8
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

"Sam Kuehn" <sa******@hotmail.com> wrote in message
news:09****************************@phx.gbl...
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?

Nov 21 '05 #9

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

Similar topics

6
by: Stuart Golodetz | last post by:
Hi, I've got a minor casting issue which I need to check about (marked // <--). I was trying to do a static_cast on it (which didn't work, though I'm not sure why this should be the case?) I...
4
by: Kevin L | last post by:
Below is a code snippet that fails to compile under vc.net class BaseInterface { public: virtual void f() = 0; }; class BaseImplementation : public BaseInterface {
10
by: Jakob Bieling | last post by:
Hi, Suppose I have a base class and a derived class as follows: class base { public: void somefunc (); };
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...
12
by: Mike | last post by:
I want to provide an abstract class to derive all nodes from. In the example below Parent in the base class is of Type Node, but in the custom class I want Parent to return a Type CustomNode. When...
6
by: apm | last post by:
Recently I have had to use a value type for a complex structure because I don't know how to override the = operator. Can the operator ever be overloaded? Or can inheritance be used with value types?
1
by: vinoraja | last post by:
There are a number of reasons we don't implement Multiple Implementation Inheritance directly. (As you know, we support Multiple Interface Inheritance). However, I should point out that it's...
18
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before....
3
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table:...
1
by: Eric | last post by:
Let's say I have a base class that implements IComparable<baseClass>. Really it could be any interface but I'm picking that one for the sake of discussion. In this class, equality and comparisons...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.