473,804 Members | 3,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2291
"Sam Kuehn" <sa******@hotma il.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.FromPe rson(myPerson)

Hope this may help.
"Sam Kuehn" <sa******@hotma il.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******@hotma il.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 ExtendedBookSta ts 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.FromPe rson(myPerson)

Hope this may help.
"Sam Kuehn" <sa******@hotma il.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******@hotma il.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 ExtendedBookSta ts 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.FromPe rson(myPerson)

Hope this may help.
"Sam Kuehn" <sa******@hotma il.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
ArgumentNullExc eption("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******@hotma il.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
ArgumentNullExc eption("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******@hotma il.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
6490
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 also tried reinterpret_cast (which is clearly an exceedingly dodgy thing to do; it worked, but I'm not sure whether it should have worked, or whether (the more likely scenario) it was just coincidence?) Finally, after a bit of trawling through the...
4
2580
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
2436
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
23389
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 examples?
12
1515
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 CustomNode is accessed by a helper class as Node Parent is always null. Is this a poor design, Im sure it's a common problem and I am just missing something. Note: I use virtual for Parent because abstract will not compile. public abstract...
6
3283
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
2970
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 possible for compilers to create MI for their types inside the CLR. There are a few rough edges if you go down this path: the result is unverifiable, there is no interop with other languages via the CLS, and in V1 and V1.1 you may run into deadlocks...
18
2345
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. Here's the scenario: We have a base class with all virtual functions. We'll call this the Animal class. We then make two classes Fish and Bird that both inherit from Animal. In the program, we have a single array of Animal pointers that will...
3
2906
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: tlkpColor (PK) tlkpColorID
1
1252
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 are based on a string representation of it's attributes - a subclass may add an attribute but still uses a string representation for equality and comparison. So if I can avoid it, I'd like to have all my tests and code associated with the base...
0
9589
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3000
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.