Declaring Variables
Francis Nugent Dixon
effendi at wanadoo.fr
Tue Nov 28 16:20:29 EST 2006
Hi from Paris,
I'm sorry you guys :
> From: Jan Schenkel <janschenkel at yahoo.com>
> Globval and script-local variables need to be
> declared, or you can't use them as such - the compiler
> will assume you're working with a temporary variable
> by the same name, and may even decide that the default
> value of that variable is the same as the name of the
> variable.
or
> From: Dar Scott <dsc at swcp.com>
> (Nice summary on declaring variables, Jan)
>
> On Nov 27, 2006, at 2:35 PM, Jan Schenkel wrote:
>
>> the compiler
> ...
>> may even decide that the default
>> value of that variable is the same as the name of the
>> variable.
but I think that you are pushing the envelope just a little too much,
especially if Jo is a beginner.
Please be assured that I am not knocking you, because you may know lots
more about Revolution and
Transcript than I do. But the problem is not there :
1 - Look at the question
2 - Based upon the question and the way it was asked, judge the level
of the person who asked the question.
3 - Answer the question on that level.
Let's look at the question ...........
> From: Jo - LunchnMeets at aol.com
> I've seen references to declaring or initializing variables. How and
> why
> would I do that?
This question is phrased like Jo is a relative beginner. You may have
even have scared him off by your answers.
So, assuming he is a beginner, here we go ....... !
Jo - In the cards of your stacks you can create fields containing
information. These fields can contain ANYTHING, numbers, texts,
binaries, on/off switches. You define your fields, name them, and you
put what you want in them. In your stack, you can execute command
scripts (usually executed by clicking on a button). Do what you like in
these scripts (some commands are easy to understand, some are not !). I
imagine you understand the principles of scripts.
Sometimes you need TEMPORARY BOXES (we'll get to the idea of
"temporary", and its SCOPE later), in which you can store information.
These are called variables. They are only known in scripts, and can
never be seen physically in your stack. When you exit the stack, these
variables, and their values will be lost. During current execution,
these variables will be KNOWN within a certain scope, that is, they may
be KNOWN in script "aaaa", and UNKNOWN in script "bbbb". It is for you
to decide.
Where are variables used ? ONLY in scripts that you may write. These
scripts are called when you click on buttons, click on locked fields,
or invoke by certain commands (such as "send mouseUp to button "aaaa").
Certain names must not be used, as they will confuse the run-time
compiler (they are called RESERVED names). You may find out this rule
the hard way. Always give guaranteed unique names to your variables..
Invent your own rules, and stick to them. As a general rule, you should
give pertinent names to your variables, especially global ones, so that
you can remember what they were used for, when you return to the
scripts later in time.
You can define the existence of a temporary (or LOCAL) variable, simply
by referring to it.
put 10 into MyLineCount
Within the script, you can change its value, use its value, check its
value, , move its value to another variable, move its value to a
defined card field, etc.
You can also declare a temporary (or LOCAL) variable by putting :
local MyLineCount
at the beginning of your script. This is not absolutely necessary, but
this way, you (or anybody else) will know which temporary variables you
used. You can also put comments to remind you what they were used for.
These temporary (LOCAL) variables will be AVAILABLE during the
execution of the script in which they are found. In other scripts,
reference to a variable of the same name will be a reference to ANOTHER
variable, with ANOTHER value. Even if you declare them, local variables
will still be unknown OUTSIDE the script in which they were defined.
Variables are initialized to "empty". You may have to initialize
variables (especially arithmetic variables), before performing
operations on them. Otherwise, the first command which uses them may
fail :
Example : put 0 into MyLineCount ........... used for arithmetic
commands
Later : add 1 to MyLineCount .............. will NOT provoke an
error.
If you want to "PASS" variable information between different scripts,
then you define the variables as GLOBAL, in each script where you wish
to refer to the values :
Example : global MyFileName
If you set the variable in one script, its value will be available in
other scripts that have ALSO defined it as a global. Of course, the
time factor is important.. When you refer to to a global variable in a
script, you will get the LATEST value that was set (or empty, if it was
not yet set) in any PREVIOUSLY executed scripts that set it. If you
define a variable as global, you can see it at any time by displaying
the "Variable Watcher", assuming that you gave it a value at some time.
Now, Jo, this is not the whole story, because there is MUCH more to
variables than I have written here. But if you ARE a beginner, then
these are the basics.
And of course, Jo, we all know that the best way to explain things like
this is by giving examples. Contact me off-forum, and I will be happy
to give you some.
-Francis
"Nothing should ever be done for the first time .... but it often is !"
More information about the use-livecode
mailing list