Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Can an object know of it's existence?
Message
De
03/12/2003 13:38:59
 
 
À
03/12/2003 07:07:31
Information générale
Forum:
Visual FoxPro
Catégorie:
Programmation Orientée Object
Divers
Thread ID:
00855003
Message ID:
00855553
Vues:
23
This message has been marked as a message which has helped to the initial question of the thread.
> "The" name? I don't follow you here. Is this some form of test? :-)

I was merely trying to point out the flawed logic behind your question, and that the question you did ask was not the one that you wanted answered. ;-) Specifically:

> > > ox = CREATEOBJECT("myclass")
> > > I need the "ox" in the INIT.

From what you told us it is apparent that you need globally reachable names for the object instances (for the callbacks) but customers of your class are more likely to use local references - local variables, object properties, whatever. So the simple solution is to create unique global names for callback purposes which the clients of the class don't need to know unless they want to; "g_JimsTimer" + SYS(2015) should do nicely. However, the clients need to tell the object when it is no longer needed so that the object can unregister the callback and clean up the global reference.

If you want a global callback and automatic cleanup when the client's reference goes away then it gets a little tricky, because normally the object will not be destroyed while the global reference is still there. It is possible - you just need an additional layer of indirection - but it is not exactly straightforward.

As regards the article with Fernando's solution - I just browsed the thread again but couldn't find it either. I guess I must have confused some other thread with this one, but I am fairly sure that it was Fernando's example that I liked most. *g* Be that as it may, the basic idea of the solution was similar to this:
define class CFoo as whatever

   c_GlobalRef = ""
   l_ReleaseGlobalRef = .f.

   function Init (cOptionalGlobalName)
      if empty(m.cOptionalGlobalName)
         this.c_GlobalRef = "g_JimsTimer" + sys(2015)
         public (this.c_GlobalRef)
         this.l_ReleaseGlobalRef = .t.
      else
         this.c_GlobalRef = m.cOptionalGlobalName
      endif
      store this to (this.c_GlobalRef)
      * ...

   procedure Done
      * unregister callback ...
      * ...
      if this.l_ReleaseGlobalRef
         release (this.c_GlobalRef)
      endif

enddefine
which allows clients to specify a global name if they so choose but does not require them to do so.

> > = createobject("custom")
> > with createobject("custom")
> > * ...
> > endwith
> > return createobject("custom")


> Without the assignment, of what use would the above be?

Anonymous object instances do have their uses, and the 'target-less' createobject() call is one form of the classic pre-VFP8 exception frame. Grossly simplified examples:
local nError
= createobject([CTryEraseFile_], m.cFileName, @m.nError)
if m.nError == 0
   with GetMeACat()
      .purr
      .meow
      .hiss
   endwith
endif
* ...

function GetMeACat
   * ...
   return createobject([CCat])

define class CTryEraseFile_ as relation

   n_Error = 0

   function Init (cFileName, rnError)
      erase (m.cFileName)
      rnError = this.n_Error
      return .f.

   procedure Error (nError, cMethod, nLine)
      this.n_Error = m.nError

enddefine
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform