Local/Global confusion

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
NigelS
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 83
Joined: Sat Oct 22, 2011 2:37 pm

Local/Global confusion

Post by NigelS » Wed May 08, 2013 5:28 pm

I'm a little confused with Local and Global variable declaration. My understanding is as follows, which is what the manual explains, if a Local variable is declared at the top of the card script then that Local variable is seen with a function or command block within that card script and does not have to be declared . For a Global variable, within the Function or Command block the Global variable has to be declared otherwise it's not seen.

My experience has been exactly the opposite.

Code: Select all


global lgPropertyTypeID, lgObjectTypeList, lgPropertyTypeList, lgPropertySubTypeList, lgPropertyDetailList
global lgPropertySubTypeArray, lgPropertyDetailArray 
global ldgLabelArray, ldgObjectType


the global variable lgPropertySubTypeList is used in this command and works fine without having declared it.

Code: Select all


command createVariousDataContainers pOptionName
   
   local tSql
   local tSqlQuery, tConnectionID, tSqlResult, tLoginFailed 
   
   put false into tLoginFailed
   
   if GetDataBaseID() is empty then
      openDatabase GetPlatformDesc()
   end if 
   
   put GetDataBaseID() into tConnectionID
   
   switch pOptionName
      case "PropertyDetailsType"
         put "SELECT * FROM propertydetailstype" into tSqlQuery
         break
      case "PropertyType"
         put "SELECT * FROM propertytype" into tSqlQuery
         break
      case "PropertySubType"
         put "SELECT * FROM propertysubtype" into tSqlQuery
         break
   end switch
   
   put revDataFromQuery(,,tConnectionID,tSqlQuery) into tSqlResult
   if tSqlResult begins with "revdberr" then
      put true into tLoginFailed
   else
      
      switch pOptionName
         case "PropertyDetailsType"
            put tSqlResult into lgObjectTypeList
            break
         case "PropertyType"
            put tSqlResult into lgPropertyTypeList
            populatePropertyOptionMenu
            break
         case "PropertySubType"
            put tSqlResult into lgPropertySubTypeList
            break
      end switch
      
   end if 
   
   closeDatabase
   
   return tLoginFailed
   
end createVariousDataContainers

Yet when I use a Local variable declared the application complains.... I'm scratching my head.
Attachments
Screen Shot 2013-05-08 at 5.59.09 PM.png

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Local/Global confusion

Post by dunbarx » Wed May 08, 2013 7:14 pm

Hi.

Not sure what you have, but this is how it will work.

Place your global declaration (it is actually a command) at the top of every script that contains handlers that will need it. If you load it in one place, it will be available everywhere else. At the top means above all handlers.

For what are known as script local variables, again, place them at the top, not in a handler. Then every handler IN THAT SCRIPT can access it. You might think of these as global variables, but restricted to the script that holds them.

The declarations must be above the handlers that use them. Could this be your issue?

If you insert the declaration inside handlers, you localize them, and they must be declared explicitly in each handler that needs them.

Craig Newman

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Local/Global confusion

Post by mwieder » Wed May 08, 2013 7:50 pm

What Craig said.

Variables have different scope depending on how they're used.
If you declare a variable as local inside a handler then it's only available within that handler.

Code: Select all

on someHandler
  local tVariable -- only available from within someHandler
  put 3 into tVariable
end someHandler
If you declare a variable as local outside a handler then it's available to all handlers in that script. But not to other scripts.

Code: Select all

local sVariable -- available from any handler in this script
on someHandler
  local tVariable -- only available from within someHandler
  put sVariable into tVariable
end someHandler
If you declare a variable as global then its value will be available to scripts in other objects (as long as you declare it there as well).

Code: Select all

global gGlobalVariable -- a value as already been put in here by another script
on someHandler
  local tVariable
  put gGlobalVariable into tVariable
end someHandler
If you use a variable without explicitly declaring it (lazy declaration) then... er... I forget. I think it becomes available anywhere in the script, which is probably why you get the error. You're already using the variable with a scope of the entire script, and then you try to declare it again. The compiler already knows about that variable, so it tells you about the redeclaration. If you move the variable declaration up to the top of the script the error will probably go away.

Post Reply