The scope of a global variable

Scott Rossi scott at tactilemedia.com
Sun Jan 18 22:16:03 EST 2004


On 1/18/04 6:46 PM, "Doug Lerner" <doug at webcrossing.com> wrote:

> The docs say "The scope of a global variable is every handler in the
> application."
> 
> I am not finding that to be true. If a global is declared in the main stack
> hierarchy it may not be accessible in a cloned stack with the same
> declaration unless you make the cloned stack into a substack of the main
> stack.

AFAIK, a global value is global to the engine.  Someone else can chime in
here if this is not correct, but for all intents and purposes you can verify
this for yourself.

Create a new main stack.  Set the destroyStack aqnd destroyWindow properties
to true just to be "thorough".  Create a button in the stack.  Give the
button a script like:

 on mouseUp
   global gTest
   put "still here" into gTest
 end mouseUp

Click the button once and close the stack.  Then type "answer gTest" in the
message box.  You should see "still here" answered back.  You can also
create a new main stack, add a button with a script set to:

 on mouseUp
   global gTest
   answer gTest
 end mouseUp

Again you should see "still here" in the answer window.

Note that since globals are indeed global and hang around after use, Rev
provides a delete variable command that allows you to remove variables from
memory.  If you are not finding this to be the case, you may not be
declaring your variables correctly.

Regards,

Scott Rossi
Creative Director
Tactile Media, Development & Design
-----
E: scott at tactilemedia.com
W: http://www.tactilemedia.com



More information about the use-livecode mailing list