I haven't been in OO all that long but ...
I would like to upcast to a new object type and add functionality
(methods only).
The runtime cast opertion fails in the sample below. Is there a way
to do it explicitly? Conceptually it is straightforward in cases such
as this where no data is added or shielded from the base class in the
derived class.
(This is only a sample. I realize that the method could be put in the
base class but the actual scenario involves a base class from the .NET
framework)
public class Props
{
public int NumberB;
public int NumberC;
}// public class Props
public class CustomProps : Props
{
public int BxC()
{
return NumberB * NumberC;
}
}
Props prop = new Props();
prop.NumberB = 4;
prop.NumberC = 5;
//this fails
CustomProps custprop = (CustomProps)pr op;
int result = custprop.BxC ();
I would like to upcast to a new object type and add functionality
(methods only).
The runtime cast opertion fails in the sample below. Is there a way
to do it explicitly? Conceptually it is straightforward in cases such
as this where no data is added or shielded from the base class in the
derived class.
(This is only a sample. I realize that the method could be put in the
base class but the actual scenario involves a base class from the .NET
framework)
public class Props
{
public int NumberB;
public int NumberC;
}// public class Props
public class CustomProps : Props
{
public int BxC()
{
return NumberB * NumberC;
}
}
Props prop = new Props();
prop.NumberB = 4;
prop.NumberC = 5;
//this fails
CustomProps custprop = (CustomProps)pr op;
int result = custprop.BxC ();
Comment