Page 1 of 1
Beginner's problem with variables
Posted: Sun Nov 16, 2014 3:11 am
by stephenlewis
Hi Guys
I’m a complete novice with Livecode and I’ve just started exploring variables. However I cannot seen to get this to work, and I can’t suss out what I’m doing wrong, so I’d be grateful of any help. I have a main stack called APPDMS. In the stack script I have defined a global variable gvarconnum like this;
Global
gvarconnum
On the first card I have a field called PSB-con-num which contains the following script;
on returninfield
end returninfield
on enterinfield
end enterinfield
on textChanged
put me into field "PSB-Con-num-title"
end textChanged
on textChanged
put me into gvarconnum
end textChanged
The first ‘on textChanged’ works fine. On the second card I have a field into which I want to put gvarconnum using this script for the field;
on preOpencard
put gvarconnum into me
end preOpencard.
When I open the second card the field displays the word gvarconnum rather than the value entered on the first card. What am I doing wrong?
Thanks
Stephen
Re: Beginner's problem with variables
Posted: Sun Nov 16, 2014 3:34 am
by Simon
Hi Stephen,
Welcome to the forum
There is a not so apparent thing about globals... they must be declared everywhere they are used.
But first the word global and the variable should be on one line e.g.
to add more you just put a comma in
Code: Select all
global gvarconnum,gMyvar,gTimeRightNow
Now in the field and on the second card you must declare the global
Code: Select all
global gvarconnum
on returninfield
end returninfield
on enterinfield
end enterinfield
on textChanged
put me into field "PSB-Con-num-title"
end textChanged
on textChanged
put me into gvarconnum
end textChanged
and then in the second card.
Lots of fun ahead!
Simon
Re: Beginner's problem with variables
Posted: Sun Nov 16, 2014 4:39 am
by stephenlewis
Simon.
Many thanks for the quick reply. I've declared the global gvarconnum in the script for the stack, both cards and both fields, but without success. I'll put it down for a few hours and when I come back to it my error my be obvious.
Stephen
Re: Beginner's problem with variables
Posted: Sun Nov 16, 2014 4:45 am
by Simon
oops!
I totally missed you have 2 on textChanged
No no
Code: Select all
on textChanged
put me into field "PSB-Con-num-title"
put me into gvarconnum
end textChanged
just use the one.
Simon
Re: Beginner's problem with variables
Posted: Sun Nov 16, 2014 4:51 am
by Simon
hmmm and
Code: Select all
global gvarconnum
on preOpencard
put gvarconnum into field "theFieldWhereIWantIt"
end preOpencard.
use that in the card script.
Not sure that preOpenCard gets sent to a field.
Simon
Re: Beginner's problem with variables
Posted: Sun Nov 16, 2014 5:14 am
by stephenlewis
Works a treat now Simon. Thank you.
Stephen
Re: Beginner's problem with variables
Posted: Sun Nov 16, 2014 5:28 am
by Simon
Hi Stephen,
Great photos!
I lived in Wales for a few years a very long time ago.
Glad the variable is working for you.
Simon
Re: Beginner's problem with variables
Posted: Sun Nov 16, 2014 5:00 pm
by jacque
Simon wrote:.
Not sure that preOpenCard gets sent to a field.
Yeah, it would work.
Re: Beginner's problem with variables
Posted: Thu Nov 20, 2014 1:29 am
by stephenlewis
HI Guys
Quick update on this. I want the variables to be available throughout the stack all the time, not just when the text changes, so I've put the following script in the main stack;
Code: Select all
global gvarconnum,gvarclientname,gvarcontitle,gvarclientcon,gvarappcon
on openStack
put field "PSB-con-num" into gvarconnum
put field "PSB-client-name" into gvarclientname
put field "PSB-Con-title" into gvarcontitle
put field "PSB-client-con" into gvarclientcon
put field "PSB-APP-con-name" into gvarappcon
end openStack
Works a treat now. Thanks for your input.
Re: Beginner's problem with variables
Posted: Thu Nov 20, 2014 2:06 am
by FourthWorld
Or if you're a lazy typist you can store all those values in one array:
Code: Select all
global gvarVA
on openStack
repeat for each item tItem in "con-num,client-name,Con-title,client-con,APP-con-name"
put fld "(PSB-"& tItem) into gvarA[tItem]
end repeat
end openStack
Re: Beginner's problem with variables
Posted: Thu Nov 20, 2014 12:16 pm
by Havanna
Ah, that lazy approach is lovely

Re: Beginner's problem with variables
Posted: Thu Nov 20, 2014 4:09 pm
by FourthWorld
LiveCode's associative arrays are wonderful things, useful in so many contexts - we've barely scratched the surface here. And if you need to store their contents, we have arrayEncode and arrayDecode so that they can be written to and read from disk as well. So many things become simple and efficient with arrays....