Local Variables with Variable Checking
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Local Variables with Variable Checking
It is my understanding that Local Variables are local to the handler where the are defined. Thus, if I want to call my counter, cntr, I can use that same variable name in many handlers, without a conflict, as long as they are all defined as Local.
So, I've got Variable Checking turned on. But I still get this error. Any idea why?
So, I've got Variable Checking turned on. But I still get this error. Any idea why?
Re: Local Variables with Variable Checking
You might confirm that you don't have it also defined as a script local variable, or a global or constant elsewhere.
Re: Local Variables with Variable Checking
You were right! It was accidently defined as a Global variable.
On my long list of GREAT things about LiveCode is the the Find & Replace window.
So often when you want to change a name, you absolutely must be able to find
all occurrences in the entire project. Find and Replace makes this easy.
On my long list of GREAT things about LiveCode is the the Find & Replace window.
So often when you want to change a name, you absolutely must be able to find
all occurrences in the entire project. Find and Replace makes this easy.
Re: Local Variables with Variable Checking
townsend...
you don't have to define the local variable in 'every' handler... consider this... put this script in a button, click on the button and see what happens
The local variable is just declared once at the top of the script... So, in this example, as long as you don't remove the cursor from within the rect of the button the local variable 'counter' will increment...
be well
Dixie
you don't have to define the local variable in 'every' handler... consider this... put this script in a button, click on the button and see what happens
Code: Select all
local counter
on mouseEnter
put 0 into counter
end mouseEnter
on mouseUp
add 1 to counter
put counter
end mouseUp

be well
Dixie
Re: Local Variables with Variable Checking
Good point Dixie!
So-- if a Local variable is declared at the top of a script,
then it is global to all handlers in that script.
And of course, if a Local variable is declared within a handler,
then it is unique and available only to that handler.
So-- if a Local variable is declared at the top of a script,
then it is global to all handlers in that script.
And of course, if a Local variable is declared within a handler,
then it is unique and available only to that handler.
Re: Local Variables with Variable Checking
townsend,
If you don't declare the script local variable at the top of the script, outside and before all other handlers, e.g. you declare the script local variable outside and before the fifth handler only handlers following the fifth handler can access the script local variable. Handler 1 to 4 can not access the script local variable.
I've seen scripts doing that but I find that utterly confusing and hard to track down in debugging.
I always declare script local variables at the top.
additionally I always prepend script local variable names with an s (as in script). That makes it easy to spot the use of a script local variable in different handlers.
for more on naming conventions have a look at Richard Gaskin's site
http://www.fourthworld.com/embassy/arti ... style.html
I found that very helpful
Kind regards
Bernd
one thing about "script local variables" as they are usually called as opposed to "local variables", thus indicating the scope of the two types of variables (the first for the whole script the second only within a handler):So-- if a Local variable is declared at the top of a script,
then it is global to all handlers in that script.
And of course, if a Local variable is declared within a handler,
then it is unique and available only to that handler.
If you don't declare the script local variable at the top of the script, outside and before all other handlers, e.g. you declare the script local variable outside and before the fifth handler only handlers following the fifth handler can access the script local variable. Handler 1 to 4 can not access the script local variable.
I've seen scripts doing that but I find that utterly confusing and hard to track down in debugging.
I always declare script local variables at the top.
additionally I always prepend script local variable names with an s (as in script). That makes it easy to spot the use of a script local variable in different handlers.
for more on naming conventions have a look at Richard Gaskin's site
http://www.fourthworld.com/embassy/arti ... style.html
I found that very helpful
Kind regards
Bernd
Re: Local Variables with Variable Checking
thanks Bernd... I was just sitting back thinking how to reply to townsends questions... and you have done it for me with an eloquence that would have escaped me...
you've been missing !
Dixie

you've been missing !
Dixie
Re: Local Variables with Variable Checking
thanks Dixie,
I am not really away, just a bit busy with mundane tasks...
Still enjoying the forum and the list
Kind regards
Bernd
I am not really away, just a bit busy with mundane tasks...
Still enjoying the forum and the list
Kind regards
Bernd