DigDeep Posted January 10, 2016 Share Posted January 10, 2016 Hi,I am facing some issues with Global variables in my code.If I have assigned a variable as GLOBAL at the top of the code, is it again required to re-declare the same as GLOBAL $Variablename in between the code whereever I am using $Variablename?I am finding out that I am not declaring the Global again in between the code, then it does not do the checks. Link to comment Share on other sites More sharing options...
DigDeep Posted January 10, 2016 Author Share Posted January 10, 2016 (edited) just to give a small example...Say if I have given as per below, if I am not declaring Global $Variable1 = Filepath before the re-check, it gives me result as Failed instead of Passed, even if the Func did worked. Global $Variable1 = Filepath Func First_Part() Code here... Endfunc While 1 Select Global $Variable1 = Filepath Case Fileexists = $Variable1 First_Part() Endselect Select Case Fileexists = $Variable1 ; Do re-check MsgBox(0, "", "Passed") Case else MsgBox(0, "", "Failed") Endselect Wend Edited January 10, 2016 by DigDeep Link to comment Share on other sites More sharing options...
InunoTaishou Posted January 10, 2016 Share Posted January 10, 2016 I would check out declaring variables from the help filehttps://www.autoitscript.com/autoit3/docs/keywords/Dim.htm Link to comment Share on other sites More sharing options...
czardas Posted January 10, 2016 Share Posted January 10, 2016 (edited) You only have to declare a global variable once. The reproducer code you posted does not run and it does not clearly demonstrate your problem.Global $Variable1 = "before" MsgBox(0, "Msg1", $Variable1) $Variable1 = "after" ; do not edeclare a global variable MsgBox(0, "Msg2", $Variable1)In your code, the Select statement is also wrong. You should follow Select immediately with the first Case statement. In addition, do not declare variables in a loop unless you know what you are doing. Edited January 10, 2016 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
DigDeep Posted January 10, 2016 Author Share Posted January 10, 2016 @czardas It was just a dummy example I was giving above to show what I was doing. What I wanted to mention was that when am declaring a variable as Global at the top of the script, if I do not re-declare within the While loop inside the Case statements, instead of Passed it gives me a Failed message.When I tried to re-declare again inside every "Case", it works good.just wanted to know how and where to declare a Global variable that I do not have to re-declare them again within func or inside loop multiple times.also, what is the difference between Global and Dim? Link to comment Share on other sites More sharing options...
czardas Posted January 10, 2016 Share Posted January 10, 2016 (edited) If I try and run your code, I get failed code execution messages, so I have no idea how to fix your problem. I will repeat - never redeclare a global variable.Dim is a depreciated keyword, but remains in the language for backwards compatibility. Some people still use it to empty the contents of an array. Dim can be either local or global.Why not post all your code so we can look for your problem? Edited January 10, 2016 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
TheSaint Posted January 10, 2016 Share Posted January 10, 2016 (edited) In short, once you declare a variable as Global, then whatever value you assign to the variable afterward is available to code anywhere in your script ... and you can keep changing it, and that new value is global automatically.For me, this where the simplicity of just declaring a variable at the start of your script, without assigning anything to it, is no doubt less confusing to a beginner. You do however reduce your code and typing by combining both.Global $Variable1 $Variable1 = Filepath Func First_Part() Code here... Endfunc While 1 Select $Variable1 = Filepath Case Fileexists = $Variable1 First_Part() Endselect Select Case Fileexists = $Variable1 ; Do re-check MsgBox(0, "", "Passed") Case else MsgBox(0, "", "Failed") Endselect WendLook at that declared variable on its own as a placeholder that is Global, which can have a value assigned to it at any time, and is part of the global arrangement.But in your case, you could just do the followingGlobal $Variable1 = Filepath Func First_Part() Code here... Endfunc While 1 Select $Variable1 = Filepath Case Fileexists = $Variable1 First_Part() Endselect Select Case Fileexists = $Variable1 ; Do re-check MsgBox(0, "", "Passed") Case else MsgBox(0, "", "Failed") Endselect WendI have not checked for any errors in the rest of your code. Edited January 10, 2016 by TheSaint Make sure brain is in gear before opening mouth! Remember, what is not said, can be just as important as what is said. Spoiler What is the Secret Key? Life is like a Donut If I put effort into communication, I expect you to read properly & fully, or just not comment. Ignoring those who try to divert conversation with irrelevancies. If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it. I'm only big and bad, to those who have an over-active imagination. I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage) Link to comment Share on other sites More sharing options...
czardas Posted January 10, 2016 Share Posted January 10, 2016 I missed something earlier.Case Fileexists = $Variable1 ; this is nonsense Case FileExists($Variable1) ; this is the correct test operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now