Página 1 de 1

Recursive classes?

Publicado: Dom Ago 14, 2011 11:36 pm
por DC
Can a second instance of a class be created, inside the class itself,
without pointing to the same instance that was already created?
Let's say you have a form, which creates an instance of a class, and
then displays the properties for that class, on the form. So, you have
one instance of the class, so far. Call it oInstance01
Now, inside the class, there are properties which need to be processed,
by generating another instance of the same class.
But it seems as if this second instance, is merely a pointer to the
first instance, because when it's created, it seems to be populated with
the same processed values as the first instance.
For example, inside a form, you have:
*******************************************
// frmShowPropertiesOfDisAndDat
METHOD ProcessDisAndDat( oSender ) CLASS TForm1 // Button Process
Click event
....
....
Local oDisAndDatInstance := DisAndDatClass():New()
Local aSecondProperties := oDisAndDatInstance:aSecondProperties
....
oDisAndDatInstance:Process()
....
....
::edtFirstProperty:Value := oDisAndDatInstance:sFirstProperty
For nThSecondProperty := 1 To nSecondProperties
sNthSecondProperty := aSecondProperties[ nThSecondProperty ]
::cboListOfSecondProperties:AddItem( sNthSecondProperty )
Next nThSecondProperty
....
....
Return Nil
....
....
*******************************************
Now, inside the class itself, you have a string property which also
needs to be process by the same class machinery. So, inside the class,
you have
*******************************************
#include "Xailer.ch"
#include "Boole.ch"
CLASS DisAndDatClass
DATA sSolution INIT ""
DATA sFirstProperty INIT ""
DATA aSecondProperties INIT {}
....
....
METHOD SolveSecondaryEquation( nIndexOfLiteral ) CLASS DisAndDatClass
Local sSolution := ""
Local oAnotherInstance := DisAndDatClass():New()
sSolution := oAnotherInstance:sSolution
Return sSolution
....
....
*******************************************
So, the question is, will the initial instance oDisAndDatInstance point
to the same static memory space as oAnotherInstance? Or is there a
workaround for this?

Recursive classes?

Publicado: Lun Ago 15, 2011 9:23 pm
por jfgimenez
DC,
I'm affraid I don't understand your question, but there is one thing I can
say to clarify it: every time you execute the 'class function' (the function
which has the name of the class), a new object's instance is created. So,
when you do this:
> Local oDisAndDatInstance := DisAndDatClass():New()
or this:
> Local oAnotherInstance := DisAndDatClass():New()
a new object is created which belongs to the same class DisAndDatClass. If
you want to get always the same object, that is, a only-one object from that
class, it's known as the 'singleton pattern', and may be implemented so:
FUNCTION GetAnObjectFromThisClass()
STATIC oObj
IF oObj == Nil
oObj := ThisClass():New()
ENDIF
RETURN oObj
--
Regards,
Jose F. Gimenez
http://www.xailer.com
http://www.xailer.info

Recursive classes?

Publicado: Lun Ago 15, 2011 11:20 pm
por Guest
Thanks Jose,
But I want to get 2 or more different instances of the class. The 2 or
more instance, I want to instantiate from INSIDE the class.
So, outside the class, we create an instance. Then inside the class, we
create another instance.
The problem is, when I create the 2nd instance, all of the properties
are already populated, with the values of the first instance.

Recursive classes?

Publicado: Mar Ago 16, 2011 12:53 pm
por jlalin
Hi,
did you try something like:
CLASS TTest
DATA oTest
CLASSDATA nCount INIT 0
METHOD New() CONSTRUCTOR
ENDCLASS
METHOD New() CLASS TTest
IF ::nCount++ < 10
::oTest := TTest():New()
ENDIF
RETURN Self
Each time you call TTest():New() it returns a TTest object and creates
another one in TTest:oTest
With this approach you need to take care on how many instances there are in
order to avoid infinite recursive stack calls.
Regards,
José Lalí­n

Recursive classes?

Publicado: Mié Ago 17, 2011 9:17 pm
por DC
Yes, this might. Ok, I'll try it tonight, thanks Jose.
On 8/16/2011 6:53 AM, José Lalí­n wrote:
> Hi,
>
> did you try something like:
>
> CLASS TTest
>
> DATA oTest
> CLASSDATA nCount INIT 0
>
> METHOD New() CONSTRUCTOR
>
> ENDCLASS
>
> METHOD New() CLASS TTest
>
> IF ::nCount++ < 10
> ::oTest := TTest():New()
> ENDIF
>
> RETURN Self
>
> Each time you call TTest():New() it returns a TTest object and creates
> another one in TTest:oTest
>
> With this approach you need to take care on how many instances there are
> in order to avoid infinite recursive stack calls.
>
> Regards,
> José Lalí­n
>
>

Recursive classes?

Publicado: Lun Ago 22, 2011 12:43 am
por Guest
Tried it a couple of days ago, it worked, thank you Jose.