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

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 = (ClassC)(ClassA)classb; //this doesn't work but you
get the point.


Thanks!!
 
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 = (ClassC)(ClassA)classb; //this doesn't work but you
get the point.

Thanks!!
 

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