Local Variables with Variable Checking

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Local Variables with Variable Checking

Post by townsend » Tue Jun 26, 2012 4:32 pm

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?
Attachments
886 local variables.JPG

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Local Variables with Variable Checking

Post by sturgis » Tue Jun 26, 2012 4:57 pm

You might confirm that you don't have it also defined as a script local variable, or a global or constant elsewhere.

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: Local Variables with Variable Checking

Post by townsend » Tue Jun 26, 2012 5:33 pm

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.
885 find & replace.JPG
885 find & replace.JPG (16.14 KiB) Viewed 4708 times

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Local Variables with Variable Checking

Post by Dixie » Tue Jun 26, 2012 6:16 pm

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

Code: Select all

local counter

on mouseEnter
   put 0 into counter
end mouseEnter

on mouseUp
   add 1 to counter
   put counter
end mouseUp
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

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: Local Variables with Variable Checking

Post by townsend » Tue Jun 26, 2012 6:52 pm

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.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Local Variables with Variable Checking

Post by bn » Tue Jun 26, 2012 7:10 pm

townsend,
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.
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):
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

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Local Variables with Variable Checking

Post by Dixie » Tue Jun 26, 2012 7:16 pm

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... :D

you've been missing !

Dixie

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Local Variables with Variable Checking

Post by bn » Tue Jun 26, 2012 8:34 pm

thanks Dixie,

I am not really away, just a bit busy with mundane tasks...

Still enjoying the forum and the list

Kind regards
Bernd

Post Reply