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

B

Bob Jones

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!!
 
P

Peter Duniho

[...]
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
 
B

Bob Jones

[...]
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.
 
B

Bob Jones

[...]
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?

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:
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.

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.
 

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

Top