When clicking a button, I create a global variable called connectedStatus and setting it to having the value "0", it's the status of a network connection.
There's a button that says log in, when you click on it, it should read the status of connectedStatus and either pop up the login window, or disconnect, depending on the connectedStatus.
But I can't get it to properly read the global variable, it *always* pops up the login stack.
I've done testing and it's setting the variable correctly, but the if/then doesn't work.
What am I doing wrong here?
This is the button:
on mouseUp
if connectedStatus = "1" then
close socket "127.0.0.1:2001"
put "Disconnected" into field "statusField"
put "0" into connectedStatus
set the title of button "loginButton" to "Log in"
else
set the visible of stack "loginStack" to true
end if
end mouseUp
Global variable, what am I doing wrong?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- VIP Livecode Opensource Backer
- Posts: 327
- Joined: Sun Apr 15, 2012 1:17 am
- Location: USA
- Contact:
Re: Global variable, what am I doing wrong?
To access a global variable created elsewhere, you need to declare that variable as global.
I assume that somewhere you have the statement
that declares "connectedStatus" as a global, before you put the status into it.
From there, anywhere else that you want to access that variable (as in your on mouseUp example), you have to declare that you want to use the global variable that you already created before. You need to add the statement
also in your mouseUp handler or at the top of the script before that handler.
e.g.
If you put the "global connectedStatus" in the "on mouseUp" handler, then it is only good in that handler.
If you put the "global connectedStatus" at the top of that script page before the "on mouseUp" handler, then it is good for every handler on that script page.
I assume that somewhere you have the statement
Code: Select all
global connectedStatus
From there, anywhere else that you want to access that variable (as in your on mouseUp example), you have to declare that you want to use the global variable that you already created before. You need to add the statement
Code: Select all
global connectedStatus
e.g.
Code: Select all
on mouseUp
global connectedStatus
if connectedStatus = "1" then
close socket "127.0.0.1:2001"
put "Disconnected" into field "statusField"
put "0" into connectedStatus
set the title of button "loginButton" to "Log in"
else
set the visible of stack "loginStack" to true
end if
end mouseUp
If you put the "global connectedStatus" at the top of that script page before the "on mouseUp" handler, then it is good for every handler on that script page.
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/
Re: Global variable, what am I doing wrong?
You might want to turn on the IDE feature thats going to require you to declare all variables explicitly before you use them anywhere. I have it on, it makes life easier in my own head to see a list of variables that I am using.
What I did to make declaring variables easier, is I created one main array. And in that array I made variables, with nice readable names, and clear and concise organizing of information. It makes it so much easier to keep track of variables in the debug window......
I read stuff about Global Variables not being good to use, but I don't really get it, I have done a lot using global variables, and I haven't run into a problem with using them yet. Creating an organized array with ,eaningful names works for me.
Like for example....
MyAppCentralArray["Internal Settings"]["FullscreenOnOff"]
MyAppCentralArray["User Data"][1]["User Name"]
etc
and you always only have to declare the first variable.
global MyAppCentralArray
When performing calculations and temporary functions, use local variables, they are erased once the value has been calculated.
What I did to make declaring variables easier, is I created one main array. And in that array I made variables, with nice readable names, and clear and concise organizing of information. It makes it so much easier to keep track of variables in the debug window......
I read stuff about Global Variables not being good to use, but I don't really get it, I have done a lot using global variables, and I haven't run into a problem with using them yet. Creating an organized array with ,eaningful names works for me.
Like for example....
MyAppCentralArray["Internal Settings"]["FullscreenOnOff"]
MyAppCentralArray["User Data"][1]["User Name"]
etc
and you always only have to declare the first variable.
global MyAppCentralArray
When performing calculations and temporary functions, use local variables, they are erased once the value has been calculated.
Founder & Developer @ MakeShyft R.D.A - https://www.makeshyft.com
Build Software with AppStarterStack for Livecode - https://www.AppStarterStack.com
Save Time with The Time Saver's Toolbox - https://www.TimeSaversToolbox.com
Build Software with AppStarterStack for Livecode - https://www.AppStarterStack.com
Save Time with The Time Saver's Toolbox - https://www.TimeSaversToolbox.com
-
- Livecode Opensource Backer
- Posts: 1336
- Joined: Sun Jul 12, 2009 10:53 am
- Location: Bordeaux, France
Re: Global variable, what am I doing wrong?
Hi Tom...
Instead of using globals, I have got into the habit of using custom properties instead... but there again, 'one mans meat...'
Instead of using globals, I have got into the habit of using custom properties instead... but there again, 'one mans meat...'