Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to create an object in a class
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Classes - VCX
Divers
Thread ID:
01092108
Message ID:
01092143
Vues:
17
This message has been marked as the solution to the initial question of the thread.
Zakaria,

The associated zaTime object does instantiate. The problem is that every Person object will be referencing the same object created when the very first Person class was instantiated. And each zaTime instance is going to have the same time the very first instance got.

This is because of the way you assigned the property in the property definition part of the class definition. When the first instance of a class is created VFP caches object definitions (all the property values) in memory to speed the 2nd through Nth instances. VFP does the same thing when you assign properties on the property sheet in a visual class.

What you must do is move the createobject() call to the Init() method of your class. This will give each Person object its own zaTime object. Also in the Destroy() method you should clean up the object reference.
DEFINE CLASS Person as Custom

cName = ""
dBirthdate = {//}
oTime = null
nAge = 0

function Init()
this.oTime = createobject("zaTime")
return dodefault()

function Destroy()
this.oTime = .null.
return dodefault()

FUNCTION GetAge()
RETURN  oTime.PointOfTime - dBirthDate
ENDFUNC

ENDDEFINE


DEFINE CLASS zaTime as Custom
PointOfTime = {//::}

function Init()
this.PointOfTime = DATETIME()
return dodefault()

ENDDEFINE
Another way to think of this is any function call that can return a time of execution dependent value that is assigned to an object property must be evaluated in the Init() method.

>I want to create an object from an other in a class, but for some reason this isn't possible. I can only do this in a function of a class. For example:
>
>
>
>DEFINE CLASS Person as Custom
>
>cName = ""
>dBirthdate = {//}
>oTime = null
>nAge = 0
>oTime = createobject("zaTime") && not possible why????
>
>FUNCTION GetAge()
>
>RETURN  oTime.PointOfTime - dBirthDate
>
>ENDFUNC
>
>
>
>ENDDEFINE
>
>DEFINE CLASS zaTime as Custom
>
>m.PointOfTime = DATETIME()
>
>
>ENDDEFINE
>
>
>
>
>I was wondering why this can't be done? And what is the solution for this?
df (was a 10 time MVP)

df FoxPro website
FoxPro Wiki site online, editable knowledgebase
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform