>>>Hi,
>>>
>>>I have a class defined in a prg file (e.g. MyClass.prg). I need to subclass it so that I can replace the code in one of the methods with my custom code.
>>>
>>>How do you do it?
>>>
>>>TIA
>>
>>Dmitry,
>>
>>At the time of the subclass definition, the upper class must be in scope by a SET PROCEDURE TO MyClass.prg. You must create a new prg (assuming you don't want to change the original one) in which you just redefine what needs to be changed.
>>
>>For instance, this being MyClass.prg
>>
>>
>>DEFINE CLASS SomeClass AS Custom
>>
>> FUNCTION Method_One
>> LPARAMETERS Parm1 AS Number, Parm2 AS Number
>> RETURN m.Parm1 * m.Parm2
>> ENDFUNC
>>
>> FUNCTION Method_Two
>> LPARAMETERS Parm1 AS Number, Parm2 AS Number
>> RETURN m.Parm1 / m.Parm2
>> ENDFUNC
>>
>>END DEFINE
>>
>>
>>you may write a different MySubClass.prg with
>>
>>
>>DEFINE CLASS SomeSubClass AS SomeClass
>>
>> FUNCTION Method_Two
>> LPARAMETERS Parm1 AS Number, Parm2 AS Number
>> RETURN IIF(m.Parm2 = 0, .NULL., m.Parm1 / m.Parm2)
>> ENDFUNC
>>
>>END DEFINE
>>
>
>Hi Antonio,
>
>If I may ask you a follow up question, please.
>
>The class that I am creating, by subclassing, will be used in a Form. And the custom method that will have "my" code will have a reference to the form (e.g. thisform.) Which causes the error.
>How do I use this subclassed object in the Form so that the custom method would be able to reference the Thisform?
>
>UPDATE: I think I need to instantiate the class in the INIT of this method using NewObject().
In my view, the best way to do this is to add a property to your custom class to hold a reference to the containing object. Then either pass that object as a parameter to the Init method or use code to set it.
Init method approach:
This.NewObject("mynewobj", "mysubclass", "mysubclass.prg", "", This)
and in the subclass's Init method:
LPARAMETERS toContainer
IF VARTYPE(m.toContainer) = "O" and NOT ISNULL(m.toContainer)
This.oContainer = m.toContainer
ENDIF
...
Then, you can refer to the container in subclass code as This.oContainer.
Alternatively:
This.NewObject("mynewobj", "mysubclass", "mysubclass.prg")
This.mynewobj.oContainer = This
The reason I think this is the best approach is that it keeps the class self-contained and doesn't rely on any SET anything in setting it up.
Tamar