Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

declare global variable


  • Please log in to reply
6 replies to this topic
jasmine
  • Guests
  • Last active:
  • Joined: --
Hi everyone,
sorry I am kind of new to AHK and this forum,
can I know how do I declare global array variable using AHK?
need some help here.. Thank you so much and sorry for the trouble caused.

CodeKiller
  • Members
  • 2067 posts
  • Last active: Feb 26 2016 09:30 AM
  • Joined: 10 Jul 2008
the var are always global outside a function.
You need to use the word global inside a function to use external var :

test := Object() 
test[1] := "this is it !" 

totest1() 
totest2(test) 

msgbox % "4. " test[1]

totest1() { 
global test 
msgbox % "1. " test[1] 
}
totest2(tmptest) { 
msgbox % "2. " tmptest[1]
tmptest[1] := "this is NOT it !" 
msgbox % "3. " tmptest[1]
}

glboal is not needed if you use the var as a parameter.

jasmine
  • Guests
  • Last active:
  • Joined: --
Hi,
thank you so much for your help.

is there a way for me to so call to read the global value from the function?
I've tried this code but it seems like it gives me an error

test := Object() 
totest1() 
i=0
while (i!=10)
{
msgbox % "1. " test[i]
i:=1+1
}


totest1() { 
i=0
while (i!=10)
{
global test[i]
test[i]:= "hello" i 
} 

}


the code above is trying to display hello 1, hello 2 etc till hello 9. can anyone kindly correct me on the above code? thanks

nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
Global needs to be the first line of the function, and you can't declare a member of an array as global, only the whole thing. "global test"

  • Members
  • 234 posts
  • Last active: Dec 30 2011 11:24 PM
  • Joined: 08 Nov 2009
The
global test[i]
is declaring the variable EACH time inside the loop, hence will be reset and the function will only result in test[9] having a value.

Move it to the top of the function before i=0

Edit
nimda was too quick and Spot On re [i] not needed in declaration, I missed that one.
/Edit

nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
You cannot declare global a member of an array :!:

orly
  • Guests
  • Last active:
  • Joined: --

Global needs to be the first line of the function,

No, it only needs to be before any other reference to that variable.