Page 1 of 2
compound name with variable
Posted: Fri Sep 06, 2013 4:26 pm
by pascalh4
Hello to all,
I have a small question about the names with variable.
if I write these lines of code, the stack disappears after renaming. It's ok.
Code: Select all
on mouseUp
set the name of stack "F0" to "F2"
hide stack "F2"
end mouseUp
or
ask "donner un titre"
put it into Nvar
set the name of stack "F0" to Nvar
hide stack Nvar
If I introduce the variable to create a compound name, the following code does not work.
Code: Select all
ask "donner un titre"
put it into Nvar
set the name of stack "F0" to "F0" & " " & Nvar
hide stack "F0" & " " & Nvar
where is my mistake?
Pascal
Re: compound name with variable
Posted: Fri Sep 06, 2013 4:30 pm
by bangkok
Code: Select all
ask "donner un titre"
put it into Nvar
set the name of stack "F0" to "F0 " & Nvar
hide stack ("F0 " & Nvar)
Re: compound name with variable
Posted: Fri Sep 06, 2013 4:39 pm
by Klaus
Hi Pascal,
what the verbose Mr. bangkok wanted to express

, is that you MUST ALWAYS put parens
around ANY "compound" name to make the engine recognize the correct "compound" string!
...
ask "donner un titre"
if it = empty then exit to top
##
put it into Nvar
set the name of stack "F0" to ("F0 " & Nvar)
hide stack ("F0 " & Nvar)
...
Best
Klaus
Re: compound name with variable
Posted: Fri Sep 06, 2013 4:42 pm
by bangkok
Sorry, not really verbose indeed. Too tired

Will do better next time.
Re: compound name with variable
Posted: Fri Sep 06, 2013 4:50 pm
by pascalh4
Thank you both.
I found an alternative, but it was complicated and can be dangerous for the rest of the code.
Code: Select all
ask "donner un titre"
put it into Nvar
set the name of stack "F0" to "F0" & Nvar
put "F0" & Nvar into NCvar
hide stack NCvar
Your information allows me to be more concise and certainly more accurate.
Soon
Pascal
Re: compound name with variable
Posted: Fri Sep 06, 2013 5:01 pm
by Klaus
...
ask "donner un titre"
put it into Nvar
set the name of stack "F0" to ("F0" & Nvar)
put "F0" & Nvar into NCvar
hide stack NCvar
...

Re: compound name with variable
Posted: Sat Sep 07, 2013 8:16 am
by pascalh4
Hi Klaus
You implies that it's even better with the parentheses. That's it?
By cons, set a variable to another variable is a correct action or not?
And can it be done at several levels?
Pascal
Re: compound name with variable
Posted: Sat Sep 07, 2013 11:08 am
by Klaus
Bonjour Pascal,
pascalh4 wrote:You implies that it's even better with the parentheses. That's it?
Not better, but CORRECT!
Please get used to it in your own interest, if not, it will "bite" you when you least exspect it!
pascalh4 wrote:By cons, set a variable to another variable is a correct action or not?
Yes.
pascalh4 wrote:And can it be done at several levels?
Sorry, don't understand?
Best
Klaus
Re: compound name with variable
Posted: Sat Sep 07, 2013 12:55 pm
by pascalh4
hi Klaus
Sorry, don't understand?
a variable in a variable, in a variable,in ....
Are there limits?
Another quick question for the variables.
Should we place a variable at a specific location, so that it is recognized everywhere. (recognized by several buttons for example)
Pascal
Re: compound name with variable
Posted: Sat Sep 07, 2013 1:22 pm
by Klaus
Hi Pascal,
pascalh4 wrote:hi Klaus
Sorry, don't understand?
a variable in a variable, in a variable,in ....
Are there limits?
You mean something like:
...
put 10 into tVarA
put tVarA into tVarB
put tVarB into tVarC
...
?
Then no limits
pascalh4 wrote:Should we place a variable at a specific location, so that it is recognized everywhere.
(recognized by several buttons for example)
This is not a question of "etiquette" or something what you should or should not do, but a question of neccessity!
If you NEED the variable to be available in many objects, make the variable GLOBAL,
or LOCAL if the script permits this.
Best
Klaus
Re: compound name with variable
Posted: Sat Sep 07, 2013 2:29 pm
by pascalh4
Hi,
How to, global variable?
I read the explanations in the dictionary, but I did not understand the method.
In my project:
Action of first button
Code: Select all
create stack "S0"
set the loc of stack "S0" to (450,300)
ask "donner un titre"
if it=empty then exit to top
put it into Nvar
set the name of stack "S0" to ("S0" & " " & Nvar)
Pascal
Action of second button
Code: Select all
hide button "Nouvelle Fiche"
show button "Sauver la fiche"
put ("S0" & " " & Nvar) into tvar
put char 2 of tvar into tNum
add 1 to tNum
create stack ("S" & tNum & " " & Nvar) in stack ("S0" & " " & Nvar)
Problem: the stack ("S0" & " " & Nvar) is not recognized in the second button action.
I tried to put "Global Nvar" in different places, but without success. This is Nvar that the variable problem (apparently), because replacing it with a defined value (eg aa) everything works fine.
Re: compound name with variable
Posted: Sat Sep 07, 2013 2:38 pm
by Klaus
Hi Pascal,
you need to declare a global variable in EVERY script you want to use it!
Example:
1. A button with only one handler:
Code: Select all
on mouseup
global gGlobalVar
put 10 into tGlobalVar
end mouseup
2. In a stack script or any script with more than one handler you can declare it at the top of the script:
Code: Select all
global tGlobalVar
## Now it is avaliable in any handler in this script!
on openstack
put empty into gGLobalVar
## more openstack stuff here...
end openstack
command your_custom_handler
if gGlobalVar = empty then
## whatever ...
end if
end your_custom_handler
Best
Klaus
P.S.
Please also see my answer to the "substack without mainstack" mystery
http://forums.runrev.com/phpBB2/viewtop ... 021#p85610
Re: compound name with variable
Posted: Sat Sep 07, 2013 4:41 pm
by pascalh4
sorry I don't understand the declaration.
Code: Select all
global tGlobalVar
## Now it is avaliable in any handler in this script!
here Ok
Code: Select all
on openstack
put empty into gGLobalVar
## more openstack stuff here...
end openstack
here, why empty?
Code: Select all
command your_custom_handler
if gGlobalVar = empty then
## whatever ...
end if
end your_custom_handler
and here, I don't understand the command.
1) In my event, it would have to recognize the order that I placed in the script of the first button:
Code: Select all
ask "donner un titre"
if it=empty then exit to top
put it into Nvar
the answer comes after a click of button. the script is in the code of this button. Should there be a link to "command".
2) the terms "your_custom_handler" and "tGlobalVar" are generic terms. In my event, I can keep the term Nvar (right?)
I tried this in the code mainstack
Code: Select all
global Nvar
on preopenstack
ask "donner un titre"
if it=empty then exit to top
put it into Nvar
set the loc of stack "Outils de création" to (450,650)
end preopenstack
but it displays Nvar instead of response text, in the name.
Pascal
Re: compound name with variable
Posted: Sat Sep 07, 2013 11:24 pm
by Klaus
Hi Pascal,
mon dieu, of course that were nothing but examples on how to declare and use a global variable!
2) the terms "your_custom_handler" and "tGlobalVar" are generic terms. In my event, I can keep the term Nvar (right?)
Exactement
I tested this with a mouseup script:
Code: Select all
global Nvar
on mouseup
ask "donner un titre"
if it=empty then exit to top
put it into Nvar
## Hint: Do not put parens around a LOC or RECT
## set the loc of stack "Outils de création" to 450,650
answer Nvar
end mouseUp
Worked as exspected.
Best
Klaus
Re: compound name with variable
Posted: Sun Sep 08, 2013 6:04 pm
by pascalh4
Hi Klaus
I understood that.
Moreover(for one button action) there is no need for "global". Right?
But for my needs, the answer should be given with a second button.
A big thank you.
your explanation made me understand that it was necessary to declare the variable in all actions
In fact, does it exist a place, so that only one variable declaration affects all actions.
I tried to put it in the code of the stack with "preopenstack" but it does not work.
Thanks to you my little project begins to take shape. And I begin to understand LC more easily.
Soon
Pascal