Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to get the object value from parent form ?
Message
De
23/05/2014 18:21:19
 
 
À
23/05/2014 05:53:51
Dragan Nedeljkovich
Now officially retired
Zrenjanin, Serbia
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows Server 2008
Network:
Windows 2008 Server
Database:
Visual FoxPro
Application:
Desktop
Divers
Thread ID:
01600260
Message ID:
01600621
Vues:
65
>The interesting part is that this is as it should be, but it takes some thinking to understand why. The moment you released loParameter, the variable was gone. In the next line, you issue a loParameter=null, but that's a new private variable, completely unrelated to the previous one, and that line does practically nothing, as your loMyChild.MyPopRef is still a reference to the original one.
>
>The part which introduces confusion is when you release a variable and then use it again - not something you would regularly do (we have the local scope for automatically releasing variables), and surely something that would (at least should) look suspicious to anyone reading the code.

Yes, lots of opportunities for unwanted side effects. If an object doesn't go away even when you think you've explicitly RELEASEd it, you might think you have a memory leak.

More fun with testing:
CLEAR

loParameter = CREATEOBJECT( "Custom" )
loParameter.AddProperty( "MyString", "HelloWorld" )

lcFooBar = "FooBar"

* Objects always passed by reference, also pass lcFooBar by ref:
? "Initial local variable values:"
? loParameter.MyString, lcFooBar

?
? "Initial child object values as passed in byref:"
loMyChild = CREATEOBJECT( "MyChild", loParameter, @lcFooBar )

loParameter.MyString = "HelloWorld2"
lcFooBar = "FooBar2"

?
? "Child object values after local variables are changed:"
loMyChild.MyPrint( )
? "Why is the second child object value not FooBar2?"

* RELEASE command below does not throw any error
RELEASE loParameter, lcFooBar

?
? "After RELEASEing the local vars:"
? "VARTYPE( loParameter ) = " + VARTYPE( loParameter )
? "VARTYPE( lcFooBar ) = " + VARTYPE( lcFooBar )

loMyChild.MyPropsUpdate( )

?
? "After child object updates its own values:"
loMyChild.MyPrint( )

RETURN

DEFINE CLASS MyChild AS Custom
	MyPORef = ""
	MyVarRef = .NULL.
	
	FUNCTION Init
		LPARAMETERS toParameter, tcFooBar
		
		This.MyPORef = toParameter
		This.MyVarRef = tcFooBar
		
		=This.MyPrint( )
	
	ENDFUNC
	
	FUNCTION MyPrint
		? This.MyPORef.MyString, This.MyVarRef
		
	ENDFUNC
	
	FUNCTION MyPropsUpdate
		This.MyPORef.MyString = "HelloWorld3"
		This.MyVarRef = "FooBar3"
	
	ENDFUNC

ENDDEFINE
For objects, it looks like the key concept is you are only ever dealing with references, not the object itself. RELEASE loSomeObject only releases an in-scope reference. Once the reference count drops to zero, the GC can finally remove the underlying object.

I think I'm missing something in the above test code with the non-object memvar lcFooBar. Is there some special way to store the reference to lcFooBar that gets passed in to MyChild.Init( ) ? It looks like the = assignment is dereferencing the reference and storing the value instead.
Regards. Al

"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov

Neither a despot, nor a doormat, be

Every app wants to be a database app when it grows up
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform