OOABL Upcasting - Forum - OpenEdge Development - Progress Community
 Forum

OOABL Upcasting

This question is not answered

Morning,

I'm curious whether it is possible to cast from one type to another type that inherits the top level type. 

For example (not a great example but hey) 

From Openedge.Core.String to Package.libs.CustomString. CustomString would inherit from Openedge.Core.String.

I have tried simply casting however an "Invalid Cast" error is thrown. This is leading me to believe that this isn't the correct approach to this. Could anyone clarify how this should be handled?

I guess I could in theory have CustomString with a constructor that takes an OpenEdge.Core.String and copies over all it's properties but I wouldn't have thought it would require that sort of overhead.

Thanks,

Kim

All Replies
  • Unless the string you want to cast is really a custom one that should indeed fail even if both have the same methods/properties... that’s why we have interfaces :)

    Sent from outer space
  • Perhaps string was a poor example. So we are actually looking to take Openedge.Web.WebRequest and extend it a little. We have tried both implementing the same interfaces and inheriting from it but haven't been able to cast to our custom object in either instance.

  • As said, if the instance you have is not one of your extended class it won’t work... it works the other way around though. But why do you want to cast it in the first place? If that adds new methods to the webrequest class how do you expect a webrequest instance to be cast to your extended one if it simply doesn’t do what you need it to do?

    Sent from outer space
  • As I said previous; this could have been the wrong approach. I simply couldn't see a better method that would enable us to extend the WebRequest object. So you're suggesting that the only way to do this is to new up a new instance of the Custom Web Request and pass in what we need?

  • If you extend the webrequest then define the variable as extendedwebrequest if you have methods there that aren’t found in webrequest, otherwise let it be a plain webrequest and when you instantiate it with new extendedwebrequest you can pass that down the stream to a webclient or something that will just treat it as a plain webrequest.

    If you just get a webrequest instance then you can try to see if it’s an instanceof your extendedwebrequest, if it is you can cast it otherwise you just can’t... but again, do you really care? Any new methods there that you want to use?

    Sent from outer space
  • Why do you need to cast or new anything.  Any subclass can be treated as one of its superclasses as long as one confines oneself to the methods and properties of the superclass.  E.g., one can have a method in some class which has a parameter defined as the superclass and then call that method passing in any of the subclasses of that superclass.  And, as has been hinted, one can define the message in terms of an interface and pass in any object which supports that interface.

    Consulting in Model-Based Development, Transformation, and Object-Oriented Best Practice  http://www.cintegrity.com

  • You might have to post some code. I'm not sure why are you not able to use you custom class. Are you by any chance trying to up-cast WebRequest to your custom WebRequest class when the object was NEWed as WebRequest? You will need to NEW it as custom WebRequest and not just plain WebRequest. Like what Thomas stated, you  can treat a instance of a subclass as either instance of the subclass and superclass, but you cannot treat an instance of the superclass as an instance of the subclass.

  • I can appreciate that up-casting my not be possible. However we are given the existing Web Request from a process out of our control and ideally wanted to add a little bit of extra functionality to it.

  • Kim,
     
    If you are using PASOE and want to do this just create a class that extends ours then in your config specify your class as what we will call for the WebHandler.
     
    Brian

  • Right, so then you can get that extra functionality just by using cast… build a ‘proxy’ that takes the webrequest injected in the constructor, proxy all methods you don’t want to override/extend to the original instance and add your extra bits on top of it, if you have an interface just say you implement the contract instead of inheriting from the webrequest and overriding everything there :(


    Marian Edu

    Acorn IT 
    +40 740 036 212

  • So an external process is creating an instance of the WebRequest for you and possibly retrieve it using a property? Unless you can specify (or pass) your own instance of WebRequest, I don't think you will be able to use a custom WebRequest.

  • So I think we have solved the problem. It was within PASOE and we weren't previously aware that you could override the HandleRequest Method in the Web Handler. This seems to have solved the problem and we can now new up the custom web request. Thanks everyone for your input.