Globals Question

Raymond E. Griffith rgriffit at ctc.net
Wed Nov 2 18:13:51 EST 2005


>> Hi,
>> 
>> I'm a bit confused over the Scope of Global Symbols. If I have a stack with:
>> 
>> global gGlobal
>> 
>> defined outside of all handlers
>> 
>> and the same in a card or control script BUT in the same Stack, is
>> the same "gGlobal" used in both places?
>> 
>> Thanks a lot
>> Dave
> 
> There are two aspects to scope that affect you in this case.
> 
> 1. A global defined outside of all handlers (I presume you mean above
> all handlers) is available to all handlers within a given object, be
> it a button, card, or stack. I think you are clear about this.
> 
> 2. If the same global variable is defined in multiple objects, be it
> a button, card, or stack, it is the same global even if the objects
> are in different stacks. Globals are environment-wide global. This is
> true regardless whether a global is defined outside of all handlers
> or inside a specific handler.
> 
> Robert Brenstein

I have wrestled with the global issue for some time, and finally found a way
that (to my satisfaction at least) settles the problem.

In the stack script, I have

 global myglobalarray

 on setglobal elementname,globalvalue
   put globalvalue into myglobalarray[elementname]
 end setglobal

 function getglobal elementname
   return myglobalarray[elementname]
 end getglobal

>From this I can set a global or get a value anywhere in the stack at any
time without additional global declarations, no matter what the object is.

I freely confess that this is likely to be a bit slower than other options,
but only marginally so in the larger scheme of things. The myglobalarray is
sufficient for most globals that don't need to be arrays themselves.

Regards,

Raymond E. Griffith





More information about the use-livecode mailing list