A
Anthony Greene
This is probably a very introductory object-oriented question, but it
has been nagging me for years, and since I've never been able to find
the right answer, I've had to work around it with non-optimal code.
However, I suspect there must be a proper object-oriented approach to
solving this problem. Perhaps someone might be able to help me out.
Oftentimes I need to convert an object declared as a base type into a
object of one of its derived classes. Here is an example of my non-
optimal code (what I am doing now). Imagine that
both the base class and derive class are much more complicated than
this with dozens of properties.
/********/
class Shape : System.Object
{
public decimal XCoordinate;
public decimal YCoordinate;
}
class Circle : Shape
{
public decimal Circumference;
}
void main ()
{
//declare a new variable of type Shape
Shape MyShape = new Shape();
MyShape.XCoordinate = 99;
MyShape.YCoordinate = 99;
//various programming steps
// ...
//I now decide i need MyShape to be of type Circle
Circle MyCircle = new Circle();
//copy all properites of MyShape into MyCircle
MyCircle.XCoordinate = MyShape.XCoordinate;
MyCircle.YCoordinate = MyShape.YCoordinate;
MyCircle.Circumference = 500;
}
/********/
What I need to know is what is the best way to create a new Circle
object which is an exact copy of all properties of MyShape.
Obviously, copying all properties over from one to the other is not
ideal, because if later I add new properties of the Shape class, I
would want its values copied over to MyCircle, without updating the
above code.
When I try to execute a line like:
MyCircle = (Circle)MyShape;
That obviously does not work, and throws a runtime error: "A first
chance exception of type 'System.InvalidCastException' occurred".
However, I am sure there must be a proper approach to this, other than
manually creating a new instance and copying over each and every
property.
Thank you for your help.
has been nagging me for years, and since I've never been able to find
the right answer, I've had to work around it with non-optimal code.
However, I suspect there must be a proper object-oriented approach to
solving this problem. Perhaps someone might be able to help me out.
Oftentimes I need to convert an object declared as a base type into a
object of one of its derived classes. Here is an example of my non-
optimal code (what I am doing now). Imagine that
both the base class and derive class are much more complicated than
this with dozens of properties.
/********/
class Shape : System.Object
{
public decimal XCoordinate;
public decimal YCoordinate;
}
class Circle : Shape
{
public decimal Circumference;
}
void main ()
{
//declare a new variable of type Shape
Shape MyShape = new Shape();
MyShape.XCoordinate = 99;
MyShape.YCoordinate = 99;
//various programming steps
// ...
//I now decide i need MyShape to be of type Circle
Circle MyCircle = new Circle();
//copy all properites of MyShape into MyCircle
MyCircle.XCoordinate = MyShape.XCoordinate;
MyCircle.YCoordinate = MyShape.YCoordinate;
MyCircle.Circumference = 500;
}
/********/
What I need to know is what is the best way to create a new Circle
object which is an exact copy of all properties of MyShape.
Obviously, copying all properties over from one to the other is not
ideal, because if later I add new properties of the Shape class, I
would want its values copied over to MyCircle, without updating the
above code.
When I try to execute a line like:
MyCircle = (Circle)MyShape;
That obviously does not work, and throws a runtime error: "A first
chance exception of type 'System.InvalidCastException' occurred".
However, I am sure there must be a proper approach to this, other than
manually creating a new instance and copying over each and every
property.
Thank you for your help.