Beginner's problem with variables

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
stephenlewis
Posts: 8
Joined: Sun Nov 16, 2014 3:05 am

Beginner's problem with variables

Post by stephenlewis » Sun Nov 16, 2014 3:11 am

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
Stephen
www.landscapesofwales.co.uk

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Beginner's problem with variables

Post by Simon » Sun Nov 16, 2014 3:34 am

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.

Code: Select all

global gvarconnum
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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

stephenlewis
Posts: 8
Joined: Sun Nov 16, 2014 3:05 am

Re: Beginner's problem with variables

Post by stephenlewis » Sun Nov 16, 2014 4:39 am

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
Stephen
www.landscapesofwales.co.uk

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Beginner's problem with variables

Post by Simon » Sun Nov 16, 2014 4:45 am

oops!
I totally missed you have 2 on textChanged :oops:
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. :D

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Beginner's problem with variables

Post by Simon » Sun Nov 16, 2014 4:51 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

stephenlewis
Posts: 8
Joined: Sun Nov 16, 2014 3:05 am

Re: Beginner's problem with variables

Post by stephenlewis » Sun Nov 16, 2014 5:14 am

Works a treat now Simon. Thank you.

Stephen
Stephen
www.landscapesofwales.co.uk

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Beginner's problem with variables

Post by Simon » Sun Nov 16, 2014 5:28 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Beginner's problem with variables

Post by jacque » Sun Nov 16, 2014 5:00 pm

Simon wrote:.
Not sure that preOpenCard gets sent to a field.
Yeah, it would work.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

stephenlewis
Posts: 8
Joined: Sun Nov 16, 2014 3:05 am

Re: Beginner's problem with variables

Post by stephenlewis » Thu Nov 20, 2014 1:29 am

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.
Stephen
www.landscapesofwales.co.uk

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Beginner's problem with variables

Post by FourthWorld » Thu Nov 20, 2014 2:06 am

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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Havanna
Posts: 53
Joined: Wed May 14, 2014 3:00 pm

Re: Beginner's problem with variables

Post by Havanna » Thu Nov 20, 2014 12:16 pm

Ah, that lazy approach is lovely :D

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Beginner's problem with variables

Post by FourthWorld » Thu Nov 20, 2014 4:09 pm

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....
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply