In order for this site to work correctly we need to store a small file (called a cookie) on your computer. Most every site in the world does this, however since the 25th of May 2011, by law we have to get your permission first. Please abandon the forum if you disagree.

Para que este foro funcione correctamente es necesario guardar un pequeño fichero (llamado cookie) en su ordenador. La mayoría de los sitios de Internet lo hacen, no obstante desde el 25 de Marzo de 2011 y por ley, necesitamos de su permiso con antelación. Abandone este foro si no está conforme.

Recursive classes?

Xailer English public forum
Responder
DC
Mensajes: 135
Registrado: Lun May 16, 2011 6:06 pm

Recursive classes?

Mensaje 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?
Avatar de Usuario
jfgimenez
Site Admin
Mensajes: 5718
Registrado: Lun Abr 06, 2015 8:48 pm
Contactar:

Recursive classes?

Mensaje 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
José F. Giménez
[Equipo de Xailer / Xailer team]
http://www.xailer.com
http://www.xailer.info
Guest

Recursive classes?

Mensaje 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.
jlalin
Mensajes: 926
Registrado: Sab Dic 25, 2010 11:10 pm

Recursive classes?

Mensaje 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
DC
Mensajes: 135
Registrado: Lun May 16, 2011 6:06 pm

Recursive classes?

Mensaje 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
>
>
Guest

Recursive classes?

Mensaje por Guest »

Tried it a couple of days ago, it worked, thank you Jose.
Responder