Dynamic-New/Dynamic-Cast

Posted by Freddy Boisseau on 29-Sep-2008 10:16

I am trying to figure out the dynamic-cast and dynamic-new. I have written a class that my programmers will use normally, but I want them to be able to change the the class that is used if they need to. I also want them to be able to access any methods in the sub-class they define. Basically here is what I have right now.

DEFINE VARIABLE customClass AS sms.lib.customClass NO-UNDO.

DEFINE VARIABLE cName AS CHARACTER NO-UNDO.

update cName.

customClass = DYNAMIC-NEW cName ( ).

Now using that I can access the class and the methods in the super class, but I can not access the methods defined in the sub class. Now I know that I need to use the dynamic-cast, but I am just getting used to OO Programming and the documentation is not very clear to me. Can anyone give me pointers on how to do this?

Message was edited by:

Frederic Boisseau

All Replies

Posted by Shelley Chase on 29-Sep-2008 13:04

You actually do not need to use dynamic cast if you know the subclass type which I assume you do if you are writing specific code to access the subclass specific methods. Just downcast the super class object reference and you have an instance of the subclass (which of course you had all along - just not access to the subclass members.

-Shelley

Posted by Freddy Boisseau on 29-Sep-2008 14:41

That is the issue, I will not know the subclass, I will just know the superclass (if I have the terms correct). The programmer will be able to specify the specific subclass they will use, which will be based on the superclass that I have previously defined. Using the example that I posted, the superclass is sms.lib.customClass. I know that all the subclasses defined will be of that type, but can have other methods added to them.

So let assume that a programmer has written sms.lib.specialClass which is of the type sms.lib.customClass. In sms.lib.customClass, I have written a method called 'sayHello', and the programmer added to sms.lib.specialClass a method called 'sayGoodbye'. Now I can reference 'sayHello' using the DYNAMIC-NEW, but I can not reference 'sayGoodbye'.

How do I access 'sayGoodbye'?

Posted by jankeir on 30-Sep-2008 02:32

How do you know there is a sayGoodbye method to reference if you don't know the object is of type sms.lib.specialClass?

To be able to call sayGoodbye you need to be sure it's there.

There are only 3 ways to know:

  • The sayGoodbye method is defined in customClass (it can still be overwritten in specialClass)

  • sayGoodbye is defined in an interface implemented by specialClass (in that case cast the object to the interface type and call it from there)

  • sayGoodbye is only defined in specialClass (in that case you know the object should be of type specialClass if you want to call it.)

Posted by Admin on 30-Sep-2008 07:27

I vote for the Interface.

Another opotion might be to create a "Dispatch" method in the super class and override that in the custom class. This Dispatch method would receive a parameter for the method name and execute that method. It get's a bit tricky if there are paramters involved - But that's probably doable.

Never the less: Use Interfaces.

Posted by jmls on 30-Sep-2008 13:55

+1 for interfaces.

This thread is closed