Hi Fred,
I'm not aware of any oo languages, which can do that. You cannot upcast in
this case because run-tyme type of the obejcts is Props not CustomProps.
Speeking of c# you cannot even overload the cast operation in this case
because upcast and downcast are predifined casting operation and they cannot
be overloaded.
I just came up with this idea. It si not exactly what you want, though.
Anyway, it can lead you to a better solution.
public class Props
{
public int NumberB;
public int NumberC;
public Props(int B, int C)
{
NumberB = B;
NumberC = C;
}
}// public class Props
public class CustomProps : Props
{
public int BxC()
{
return NumberB * NumberC;
}
public CustomProps(int B, int C): base(B, C)
{
}
}
public class CustomPropsUpcastHelper
{
private Props props;
public CustomPropsUpcastHelper(Props p)
{
props = p;
}
public static implicit operator CustomProps(CustomPropsUpcastHelper
helper)
{
return new CustomProps(helper.props.NumberB, helper.props.NumberC);
}
public static explicit operator CustomPropsUpcastHelper(Props props)
{
return new CustomPropsUpcastHelper(props);
}
}
and you can use this like:
Props props = new Props(10, 20);
CustomProps customProps = (CustomPropsUpcastHelper)props;
Console.WriteLine(customProps.BxC());
Console.ReadLine();
HTH
B\rgds
100
"Fred." <fw*****@VenturiPartners.com> wrote in message
news:70**************************@posting.google.c om...
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)prop;
int result = custprop.BxC ();