Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
A basic thought about LOCAL variables
Message
 
 
À
Tous
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Titre:
A basic thought about LOCAL variables
Divers
Thread ID:
00309158
Message ID:
00309158
Vues:
56
Hello

I have been trapped today by a behaviour of VFP: it showed me that even if I think I know well enough VFP, I still can ignore some basics:

Take this code:
DEFINE CLASS clsTest AS line

	Name = "clsTest"

	PROCEDURE Init
		This.Test1()
	ENDPROC

	PROCEDURE test1
		* Create llSuccess, local by default:
		llSuccess = .F.
		If this.test2()
		*<Some code>
		endif
		? llSuccess	&& turned into .T.!
	ENDPROC

	PROCEDURE test2
		* no parameter is passed !
		* Create llSuccess, local by default:
		llSuccess = .F.
		*<Some other code>
		llSuccess = .T.
		RETURN llSuccess 
	ENDPROC

ENDDEFINE
I've always thought that a variable created in a method is by default LOCAL and cannot be accessed by higher or lower methods, that is (in my mind) that it cannot be changed by another lower / higher method. That's what I read in the Help.

And that's true most of the time, when you create a simple variable in a method. But it's false when your method calls another method in which you create a local variable with the same name.

Thus, the 'llSuccess' variable in Test1() shouldn't be affected by what happens to a same-named variable in another method like test2(). Yet it is: llSuccess is turned into .T. after test2() has executed!
But the problem doesn't occur any longer if I create llSuccess in each method this way:
LOCAL llSuccess   && .F. by default, anyway
The bottom line of this is that it's a good habit to always declare explicitly the variables LOCAL when one creates a local variable in any method.

HTH
Kind regards

Philippe RIONDEL,
a French man in Montreal
=======================================================

Who tries can sometimes be wrong, who doesn't is always

Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform