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

Copying a derived type to another type that inherits from derived type

Ok, I am curious if this can be done in C# 2.0 via some internal .net
functionality or if I have to do a coversion myself such as a copy
construtor or override the explicit operator.

I have 3 classes.

ClassA
ClassB which derives from ClassA
ClassC which derives from ClassA as well

I can implicitly convert to ClassA from ClassB and ClassC. Easy enough

I would like to convert ClassB to ClassA and then create a new
instance of ClassC and use the converted ClassA to fill in the derived
methods and properties in ClassC without having to write code that
literally copies values from one class to the other. Is this possible?

Psuedo Code:

ClassB classb = new ClassB();
ClassC classc = new ClassC();

ClassB classb2 = (ClassB)(ClassA)classc; //this doesn't work but you
get the point.

Thanks!!

Jun 29 '07 #1
3 1384
On Fri, 29 Jun 2007 14:16:54 -0700, Bob Jones <go********@gmail.comwrote:
[...]
I can implicitly convert to ClassA from ClassB and ClassC. Easy enough

I would like to convert ClassB to ClassA and then create a new
instance of ClassC and use the converted ClassA to fill in the derived
methods and properties in ClassC without having to write code that
literally copies values from one class to the other. Is this possible?
No.

And you should keep in mind that when you cast from ClassC to ClassA (for
example), you are _not_ "converting" anything. You are simply changing
the view onto the instance. The instance remains a ClassC instance, and
in fact will behave exactly like a ClassC instance with respect to any
members in ClassA that ClassC overrides.

You can, of course, write constructors for ClassB and ClassC that take a
ClassA for initialization, and then copy the data from a ClassA-derived
instance to help initialize the new instance of ClassB or ClassC. But you
probably already knew that.

And if the point in my first paragraph wasn't clear enough:
Psuedo Code:

ClassB classb = new ClassB();
ClassC classc = new ClassC();

ClassB classb2 = (ClassB)(ClassA)classc; //this doesn't work but you
get the point.
What you get from the "(ClassA)classc" part of that line of code is still
a ClassC instance. You've changed how it looks, essentially "stripping
off" what the _compiler_ knows about it being a ClassC. But the instance
itself still knows it's a ClassC (for example, if you called the GetType()
method on the instance, it'd still tell you ClassC). So, just as you
can't cast a ClassC to a ClassB when the variable is typed as ClassC, you
also can't cast a ClassC to a ClassB just because the variable is typed as
a ClassA.

For that matter, you can't even cast a ClassA to a ClassB, if it's really
just a ClassA. The only way to validly cast something to a ClassB is if
it is either a ClassB, or something that derives _from_ ClassB.

Pete
Jun 29 '07 #2
On Jun 29, 5:56 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Fri, 29 Jun 2007 14:16:54 -0700, Bob Jones <goodold...@gmail.comwrote:
[...]
I can implicitly convert to ClassA from ClassB and ClassC. Easy enough
I would like to convert ClassB to ClassA and then create a new
instance of ClassC and use the converted ClassA to fill in the derived
methods and properties in ClassC without having to write code that
literally copies values from one class to the other. Is this possible?

No.

And you should keep in mind that when you cast from ClassC to ClassA (for
example), you are _not_ "converting" anything. You are simply changing
the view onto the instance. The instance remains a ClassC instance, and
in fact will behave exactly like a ClassC instance with respect to any
members in ClassA that ClassC overrides.

You can, of course, write constructors for ClassB and ClassC that take a
ClassA for initialization, and then copy the data from a ClassA-derived
instance to help initialize the new instance of ClassB or ClassC. But you
probably already knew that.

And if the point in my first paragraph wasn't clear enough:
Psuedo Code:
ClassB classb = new ClassB();
ClassC classc = new ClassC();
ClassB classb2 = (ClassB)(ClassA)classc; //this doesn't work but you
get the point.

What you get from the "(ClassA)classc" part of that line of code is still
a ClassC instance. You've changed how it looks, essentially "stripping
off" what the _compiler_ knows about it being a ClassC. But the instance
itself still knows it's a ClassC (for example, if you called the GetType()
method on the instance, it'd still tell you ClassC). So, just as you
can't cast a ClassC to a ClassB when the variable is typed as ClassC, you
also can't cast a ClassC to a ClassB just because the variable is typed as
a ClassA.

For that matter, you can't even cast a ClassA to a ClassB, if it's really
just a ClassA. The only way to validly cast something to a ClassB is if
it is either a ClassB, or something that derives _from_ ClassB.

Pete
Thanks, Pete. I figured as much but I didn't know if .Net had any
functionality that would do the property copying from one object to
another for me. I'll look for a different way.

Jun 29 '07 #3
On Jun 29, 6:03 pm, Bob Jones <goodold...@gmail.comwrote:
On Jun 29, 5:56 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:


On Fri, 29 Jun 2007 14:16:54 -0700, Bob Jones <goodold...@gmail.comwrote:
[...]
I can implicitly convert to ClassA from ClassB and ClassC. Easy enough
I would like to convert ClassB to ClassA and then create a new
instance of ClassC and use the converted ClassA to fill in the derived
methods and properties in ClassC without having to write code that
literally copies values from one class to the other. Is this possible?
No.
And you should keep in mind that when you cast from ClassC to ClassA (for
example), you are _not_ "converting" anything. You are simply changing
the view onto the instance. The instance remains a ClassC instance, and
in fact will behave exactly like a ClassC instance with respect to any
members in ClassA that ClassC overrides.
You can, of course, write constructors for ClassB and ClassC that take a
ClassA for initialization, and then copy the data from a ClassA-derived
instance to help initialize the new instance of ClassB or ClassC. But you
probably already knew that.
And if the point in my first paragraph wasn't clear enough:
Psuedo Code:
ClassB classb = new ClassB();
ClassC classc = new ClassC();
ClassB classb2 = (ClassB)(ClassA)classc; //this doesn't work but you
get the point.
What you get from the "(ClassA)classc" part of that line of code is still
a ClassC instance. You've changed how it looks, essentially "stripping
off" what the _compiler_ knows about it being a ClassC. But the instance
itself still knows it's a ClassC (for example, if you called the GetType()
method on the instance, it'd still tell you ClassC). So, just as you
can't cast a ClassC to a ClassB when the variable is typed as ClassC, you
also can't cast a ClassC to a ClassB just because the variable is typed as
a ClassA.
For that matter, you can't even cast a ClassA to a ClassB, if it's really
just a ClassA. The only way to validly cast something to a ClassB is if
it is either a ClassB, or something that derives _from_ ClassB.
Pete

Thanks, Pete. I figured as much but I didn't know if .Net had any
functionality that would do the property copying from one object to
another for me. I'll look for a different way.- Hide quoted text -

- Show quoted text -
Just for the heck of it, if the property structure is complex then
reflection can be used to copy the properties. But, as always, Keep It
Simple Stupid.

Jul 2 '07 #4

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

Similar topics

5
by: Martin Magnusson | last post by:
Hi! I have a class with a private member which is a pointer to an abstract class, which looks something like this: class Agent { public: void Step( Base* newB ); private:
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
6
by: solex | last post by:
Hello, I am trying to use serialization to copy objects. The object in question "Institution" inherits from a parent object "Party" both are marked as <Serializable()>. Initially I can copy an...
4
by: Dennis | last post by:
I am trying to set the default design proerties in a control I have derived from the Panel Class. I thought I'd found how to do it from the MSDN but the following line doesn't work: Inherits...
10
by: Julia | last post by:
Hi Please can someone explain this behaviour: I have a MustInherit Base class and a Derived class that Inherits Base and Shadows a method in the base class. If I Dim a variable of type...
15
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
8
by: Mike C# | last post by:
Suppose I have a base class "foo". Another class, "bar" derives from it. Base class "foo" has a method called "rob_the_liquor_store()", and the inherited class "bar" overrides this method with one...
3
by: keith.thornhill | last post by:
hey all, got a problem here using Visual basic .net 2005 i have two pairs of base/derived classes. lets call them Base/Derived and BaseStruct/DerivedStruct. i want to be able to instantiate a...
1
by: Bob Jones | last post by:
Ok, I am curious if this can be done in C# 2.0 via some internal .net functionality or if I have to do a coversion myself such as a copy construtor or override the explicit operator. I have 3...
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: 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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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,...

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.