Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
What's happening here ?
Message
De
27/02/2005 11:21:26
 
 
À
24/02/2005 13:15:01
Information générale
Forum:
Visual FoxPro
Catégorie:
Classes - VCX
Versions des environnements
Visual FoxPro:
VFP 8 SP1
OS:
Windows 2000 SP4
Divers
Thread ID:
00990234
Message ID:
00990991
Vues:
16
>DEFINE CLASS c1 AS CUSTOM
> o = CREATEOBJECT("c2")
>ENDDEFINE

Underscoring this thread is that it's very questionable practice to put *any* VFP expression in class constructor code, unless you really know what you're doing. Why? Because that code is "run" (such as it is) when the first object is created, and never again while that class definition is in memory.

To illustrate the point say you have a class defined as:
DEFINE CLASS Sample AS CUSTOM
  tTimeCreated = DATETIME()
ENDDEFINE
In this class the developer erroneously thinks *each object* will be stamped with its creation time, while in fact all objects will have the same value:
o1 = CREATEOBJECT("Sample")
DECLARE Sleep IN Win32API Integer
Sleep(1000)  && force a delay between object creation
o2 = CREATEOBJECT("Sample")
? IIF(o1.tTimeCreated <> o2.tTimeCreated, "Different", "Oops!")
I'm glad to see that at least VFP 9 prevents you from doing this with *objects*. In a code review, I would flag *any* use of an expression in constructor code for examination.

What you want is either containership:
DEFINE CLASS c1 AS CUSTOM
  ADD OBJECT o AS c2 
or you want an (non-container) object reference:
DEFINE CLASS c1 AS CUSTOM
  o = NULL
  FUNCTION INIT
    o = CREATEOBJECT("c2") 
-- Randy
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform