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:
01092527
Vues:
17
Hi David,

It works as you have explained only the zaTime doesn't get instantiated because I'm using foxpro 9.

I was wondering what is the use of dodefault(), what does it do?

Furthermore, what I want to do is to pass the Time object to the Person object (it's just a fictive example). Is the following the way to do this,or are there any better methods:
oPerson = createobject("Person")
oTime = createobject("zaTime")
oPerson.Init(oTime) 
oPerson.GetAge()

*** the Function init is then as follow: 
function Init(toTime)
this.oTime = toTime
return dodefault()
Thanks for your help.

>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?
Zakaria al Azhar
My blog on Actuaris.net
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform