Page 1 of 1
HOW TO - Declare variable with initial value
Posted: Thu Aug 08, 2013 8:48 pm
by palanolho
Greetings everyone...
I know that this may be a noob question but...
In other programming languages, we can declare a variable and immediately set an initial value. For instances we are setting the default app settings and we would do something like
Code: Select all
var defaultColor = "blue";
var defaultPath = "c:\my directory"
var defaultWidth = 100
var defaultHeight = 200
How can I do such sing on a stack? basically this is a stack that I'm using as a Code Library
Many thanks in advance
- Miguel
Re: HOW TO - Declare variable with initial value
Posted: Thu Aug 08, 2013 9:02 pm
by FourthWorld
In LiveCode it's not necessary to explicitly declare variables so you can just put values into them and you're off and running.
Re: HOW TO - Declare variable with initial value
Posted: Thu Aug 08, 2013 9:18 pm
by palanolho
I understand that, but how can I do this if I want to do this? lets say that I want to define the defaults of my application?
many thanks
- Miguel
Re: HOW TO - Declare variable with initial value
Posted: Thu Aug 08, 2013 9:38 pm
by Simon
Hi Miguel,
Look up "local" in the dictionary.
Simon
Re: HOW TO - Declare variable with initial value
Posted: Thu Aug 08, 2013 9:45 pm
by palanolho
OK, maybe I'm not explaining myself correctly. I'll try another way.
using my first example, could I do the following on the root of my stack (e.g., outside a function or handler)
Code: Select all
-- VARIABLES
local defaultColor = "blue";
local defaultPath = "c:\my directory"
local defaultWidth = 100
local defaultHeight = 200
-- FUNCTIONS AND HANDLERS
on mouseup
answer "hello world
end mouseup
many thanks
Re: HOW TO - Declare variable with initial value
Posted: Thu Aug 08, 2013 9:50 pm
by Simon
Do you mean:
on mouseUp
answer defaultColor
end mouseUp
and you would get "blue"?
Simon
Re: HOW TO - Declare variable with initial value
Posted: Thu Aug 08, 2013 9:55 pm
by palanolho
Exactly. Or accessing this variable from another stack to get the variable value
Re: HOW TO - Declare variable with initial value
Posted: Thu Aug 08, 2013 10:07 pm
by Simon
Ahhh
Or accessing this variable from another stack
You probably want to use a "global" variable.
Most people here will tell you a "custom property" is the "right" way to go, but for a beginner I think "global" is easier to understand.
In your stack script
Code: Select all
global defaultColor = "blue";
global defaultPath = "c:\my directory"
global defaultWidth = 100
global defaultHeight = 200
Now here is the thing that people mess up on:
Code: Select all
global defaultColor -- This must be declared everywhere you use it
on mouseUp
answer defaultColor
end mouseUp
but do spend some time figuring out how to use custom properties you may find them more to your liking.
Simon
Re: HOW TO - Declare variable with initial value
Posted: Thu Aug 08, 2013 10:08 pm
by dunbarx
In LiveCode, there are a few types of "standard" (there are other special ones) variables. They may be declared outside of handlers, but cannot be populated outside of handlers:
1- A local variable, declared and loaded when it is created:
on mouseUp
put something into yourVariable -- declared and populated. available only within the handler
answer yourVariable
end mouseUp
2- A script local variable, declared generally at the top of the script but outside and above any handler that might use it:
local yourVariable
on mouseDown
put something into yourVariable -- declared above, populated here, available to all handlers in the script
end mouseDown
on mouseUp
answer yourVariable -- you will get the contents
end mouseUp
3- A global variable, declared anywhere, and available in any script in any stack in the current session:
global globVar
on mouseUp
put 6 into globVar -- now available in any handler in any script that declares it
end mouseUp
Only takes a little getting used to.
Craig Newman
Re: HOW TO - Declare variable with initial value
Posted: Thu Aug 08, 2013 10:15 pm
by Simon
put 6 into globVar
Yeah, I think that's the way I do it not globVar = 6
Declare the globals at the top of the stack script
Code: Select all
global defaultColor,defaultPath...
on openStack
put "blue" into defaultColor
put "c:\..." into defaultPath
etc.
end openStack
Simon
Re: HOW TO - Declare variable with initial value
Posted: Thu Aug 08, 2013 10:20 pm
by palanolho
Thank you very much to all.
Now I'm clarified.
cheers!

- Miguel