Domain problem

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
drhoward
Posts: 22
Joined: Tue Dec 10, 2013 9:03 am

Domain problem

Post by drhoward » Tue Mar 17, 2015 8:22 am

I'm having trouble initializing an array in one function and then being able to access the array content from another function. Here's an example.
mainCalc is called from a button handler. mainCalc in turn calls initializeIt to initialize myArray[].
mainCalc than calls checkSomething. Unfortunately, the values in myArray that were initialized in initializeIt are not available to the function checkSomething.
I'm using LiveCode build 7.0.3

Any suggestions will be appreciated.

Thanks!

Howard

var sAB
array myArray
var myResult

function initializeIt
put 7 into myArray[1]
put 8 into myArray[2]
put 2 into myArray[3]
end initializeIt

function checkSomething pFreqA,pFreqB
put myArray[2] into sAB
-- write some code
return sAB
end checkSomething

function mainCalc
get initializeIt()
put 4 into sA
put 876 into sB
put checkSomething(sA,sB) into myResult
-- lots of code lines follow
end mainCalc

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

Re: Domain problem

Post by bn » Tue Mar 17, 2015 9:38 am

hi drhoward

when you initialize script local variables (the ones outside of any handler in a script) you use "local". And you don't put the type, Livecode does that automatically for you.
try this

Code: Select all

local sAB
local myArray
local myResult

function initializeIt
put 7 into myArray[1]
put 8 into myArray[2]
put 2 into myArray[3]
end initializeIt

function checkSomething pFreqA,pFreqB
put myArray[2] into sAB
-- write some code
return sAB
end checkSomething

function mainCalc
get initializeIt()
put 4 into sA
put 876 into sB
put checkSomething(sA,sB) into myResult
-- lots of code lines follow
end mainCalc
I find it helpful to prepend local variables with t, script local variables with s, global variables with g. That way you always know what type of variable you are dealing with.
Kind regards

Bernd

drhoward
Posts: 22
Joined: Tue Dec 10, 2013 9:03 am

Re: Domain problem

Post by drhoward » Tue Mar 17, 2015 2:28 pm

Thank you! Using local works.

Post Reply