handler local variables
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
handler local variables
Ok, I have a question that didn't occur to me before. If a variable is declared script local, it is available to all handlers and functions in that script in a persistant state.
If it is declares as a handler local variable, it does NOT seem to be persistant.
So first question, why bother declaring it as a local if its not persistant between handler calls?
Is there a way to use a variable that is persistant between handler calls that is only accessible by that handler?
I assume this is normal behavior, it just doesn't seem normal to me so i'm hoping someone can explain why it is this way.
TIA!
If it is declares as a handler local variable, it does NOT seem to be persistant.
So first question, why bother declaring it as a local if its not persistant between handler calls?
Is there a way to use a variable that is persistant between handler calls that is only accessible by that handler?
I assume this is normal behavior, it just doesn't seem normal to me so i'm hoping someone can explain why it is this way.
TIA!
Re: handler local variables
Hi Sturgis,
declaring handler local variable is not necessary when you just use them. Declaring handler local variables, also called temporary variables as in "not persistent", is a means to write clean code. If you turn on the strict compilation mode in the preferences you have to declare handler local variables and the compiler barks if you us a undeclared variable. It is also a means to avoid typos in variable names that can be hard to track down.
But you probably know all this.
A script local variable declared at the top of a script is accessible by all handlers. If you declare a script local variable later, say just above handler 6 of 10 in the script only handler 6 to 10 can access the script local variable. Handler 1 to 5 can not access that script local variable.
Now if you make the handler that should have unique access to a persistent script local variable then make that handler the last handler in the script and declare the script local variable right above that handler.
For whatever that may be good
Kind regards
Bernd
declaring handler local variable is not necessary when you just use them. Declaring handler local variables, also called temporary variables as in "not persistent", is a means to write clean code. If you turn on the strict compilation mode in the preferences you have to declare handler local variables and the compiler barks if you us a undeclared variable. It is also a means to avoid typos in variable names that can be hard to track down.
But you probably know all this.
I don't know your use case for that but you can trick your way around:Is there a way to use a variable that is persistant between handler calls that is only accessible by that handler?
A script local variable declared at the top of a script is accessible by all handlers. If you declare a script local variable later, say just above handler 6 of 10 in the script only handler 6 to 10 can access the script local variable. Handler 1 to 5 can not access that script local variable.
Now if you make the handler that should have unique access to a persistent script local variable then make that handler the last handler in the script and declare the script local variable right above that handler.
For whatever that may be good
Kind regards
Bernd
Re: handler local variables
Hey, thats a neat trick, thanks! As far as setting explicit vars, yep did know that. As far as my use case.. well I have never declared a local in a handler but was looking at code today and my brain kicked me in the shins about this until I finally asked the question. I'll surely file the trick away for future potential use!
bn wrote:
I don't know your use case for that but you can trick your way around:
A script local variable declared at the top of a script is accessible by all handlers. If you declare a script local variable later, say just above handler 6 of 10 in the script only handler 6 to 10 can access the script local variable. Handler 1 to 5 can not access that script local variable.
Now if you make the handler that should have unique access to a persistent script local variable then make that handler the last handler in the script and declare the script local variable right above that handler.
For whatever that may be good
Kind regards
Bernd
Re: handler local variables
I know that placement trick works on LiveCode server but I didn't think it applied to regular stack scripts. I'm not at a computer right now where I can test it though. Does it really work universally? If no else does, I'll try to remember to try it myself tomorrow.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: handler local variables
Hi Jacque,
you might want to try this
Kind regards
Bernd
you might want to try this
Code: Select all
on mouseDown
put sLOCAL && the milliseconds
end mouseDown
local sLOCAL = "warm weather"
on mouseUp
put return & sLOCAL && the milliseconds after msg
end mouseUp
Bernd
Re: handler local variables
That is just too cool! (ok, so i'm too easily amused) but yes indeed it does function this way.
Re: handler local variables
Thanks for confirming, good to know. A new trick. 

Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com