OO Upcasting issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fred.

    #1

    OO Upcasting issue

    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 ();
  • 100

    #2
    Re: OO Upcasting issue

    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 CustomPropsUpca stHelper
    {
    private Props props;
    public CustomPropsUpca stHelper(Props p)
    {
    props = p;
    }

    public static implicit operator CustomProps(Cus tomPropsUpcastH elper
    helper)
    {
    return new CustomProps(hel per.props.Numbe rB, helper.props.Nu mberC);
    }

    public static explicit operator CustomPropsUpca stHelper(Props props)
    {
    return new CustomPropsUpca stHelper(props) ;
    }
    }


    and you can use this like:

    Props props = new Props(10, 20);
    CustomProps customProps = (CustomPropsUpc astHelper)props ;
    Console.WriteLi ne(customProps. BxC());
    Console.ReadLin e();

    HTH

    B\rgds
    100
    "Fred." <fwalton@Ventur iPartners.com> wrote in message
    news:7002612c.0 307170908.6832f 1a0@posting.goo gle.com...[color=blue]
    > 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 ();[/color]


    Comment

    Working...