Casting issue RRS feed

  • Question

  • Hi,
    its late and I'm losing it but here goes...

    I have a class factory, that returns a base class - all objects returned from the factory are at least that class.  Now say I have a SalesOrder class and I have a derived class from SalesOrder, SpecialSalesOrder.

    In another class, I need a SalesOrder, so I code:
    public SalesOrder WorkSalesOrder
                BaseObject obj = factory.create("SalesOrder");//<- yes this takes a string parameter
                return (SalesOrder)BaseObject; //<- this is the problem line

    [without error checking] the above code works fine, but when I pass it a "SpecialSalesOrder" the returned type is a SpecialSalesOrder but the cast SalesOrder downcasts it [apparently].  Is there syntax in c# I can use, such as Type.GetType("SpecialSalesOrder") which will not downcast?

    I can not change the return type to BaseObject as this MUST return a SalesOrder, or subclass of SalesOrder.

    the returned Type is SalesOrder; I believe that if I returned object or BaseObject it would be a SpecialSalesOrder.  Is there a way to avoid the downcast?


    Wednesday, September 3, 2008 10:32 AM

All replies

  • Why do you not want to cast to SalesOrder? That's what the property's return type is, after all. Does the cast fail?

    Mattias, C# MVP
    Wednesday, September 3, 2008 10:54 AM
  • The idea of a factory is that it takes care of the properties of a SpecialSalesOrder that make it different from a normal SalesOrder.  So that the application only has to deal with the base properties of a SalesOrder.  If your application code in fact needs to be aware that it is a SpecialSalesOrder (requiring a downcast), a factory is not the proper solution.  Don't forget about virtual methods as another way to customize the behavior of a class.
    Hans Passant.
    Wednesday, September 3, 2008 11:29 AM
  • Hi,

    i guess the problem is not so clear.  Let me try again.  The problem is that the casting to make it compile is the problem - SpecialSalesOrder is a subclass, and the explicit cast casts it back to a SalesOrder.  So when I ask the return type what it it, it replies 'SalesOrder', even tho the factory returned a SpecialSalesOrder.  This code is in a helper class, that relies on the object being a SalesOrder or a subclass of SalesOrder.  I need the return object from this method, when asked what Type it is, to reply with SpecialSalesOrder.  If I returned object, that wold be the case - the returned object, from the factory, is a SpecialSalesOrder.  But this helper class requires the returned object to a SalesOrder or subclass.  I cant seem to achieve this, although I feel this is a genuine need.

    Any ideas?

    Thanks again.
    Wednesday, September 3, 2008 1:37 PM
  •  How do you check the type? obj.GetType() should return typeof(SpecialSalesOrder) if that's indeed the type the factory creates.
    Mattias, C# MVP
    Wednesday, September 3, 2008 2:23 PM
  •  Hi Mattias,

    yes, thats what I was doing, but in the debugger.  I was trying lots of things, and then spent time looking for some syntax or options to achieve this.  I will try again and carefully record the outputs.  Is there any documentation which describes the casting rules of this particular case?


    Wednesday, September 3, 2008 9:55 PM