Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
What's happening here ?
Message
From
27/02/2005 11:21:26
 
 
General information
Forum:
Visual FoxPro
Category:
Classes - VCX
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows 2000 SP4
Miscellaneous
Thread ID:
00990234
Message ID:
00990991
Views:
17
>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
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform