HOW TO - Declare variable with initial value

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
palanolho
Posts: 122
Joined: Sat Apr 27, 2013 11:40 pm

HOW TO - Declare variable with initial value

Post by palanolho » Thu Aug 08, 2013 8:48 pm

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

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

Re: HOW TO - Declare variable with initial value

Post by FourthWorld » Thu Aug 08, 2013 9:02 pm

In LiveCode it's not necessary to explicitly declare variables so you can just put values into them and you're off and running.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

palanolho
Posts: 122
Joined: Sat Apr 27, 2013 11:40 pm

Re: HOW TO - Declare variable with initial value

Post by palanolho » Thu Aug 08, 2013 9:18 pm

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

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

Re: HOW TO - Declare variable with initial value

Post by Simon » Thu Aug 08, 2013 9:38 pm

Hi Miguel,
Look up "local" in the dictionary.

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

palanolho
Posts: 122
Joined: Sat Apr 27, 2013 11:40 pm

Re: HOW TO - Declare variable with initial value

Post by palanolho » Thu Aug 08, 2013 9:45 pm

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

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

Re: HOW TO - Declare variable with initial value

Post by Simon » Thu Aug 08, 2013 9:50 pm

Do you mean:
on mouseUp
answer defaultColor
end mouseUp
and you would get "blue"?

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

palanolho
Posts: 122
Joined: Sat Apr 27, 2013 11:40 pm

Re: HOW TO - Declare variable with initial value

Post by palanolho » Thu Aug 08, 2013 9:55 pm

Exactly. Or accessing this variable from another stack to get the variable value

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

Re: HOW TO - Declare variable with initial value

Post by Simon » Thu Aug 08, 2013 10:07 pm

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

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

Re: HOW TO - Declare variable with initial value

Post by dunbarx » Thu Aug 08, 2013 10:08 pm

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

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

Re: HOW TO - Declare variable with initial value

Post by Simon » Thu Aug 08, 2013 10:15 pm

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

palanolho
Posts: 122
Joined: Sat Apr 27, 2013 11:40 pm

Re: HOW TO - Declare variable with initial value

Post by palanolho » Thu Aug 08, 2013 10:20 pm

Thank you very much to all.

Now I'm clarified.

cheers! :)
- Miguel

Post Reply