| View previous topic :: View next topic |
| Author |
Message |
Larry
Joined: 03 Mar 2005 Posts: 61
|
Posted: Sun Aug 16, 2009 8:23 am Post subject: Global and Static Variables, When are they needed? |
|
|
Although I've been using AutoHotKey for some time now, I'm still uncertain
as to when Global and Static variables need to be used.
I'm also not real clear on the difference between them.
And, are functions the only way they can be declared. Seems like I
remember seeing somewhere that variables ending with numbers
are considered array variables and are then Global by nature.
Am I correct on that?
Thanks. |
|
| Back to top |
|
 |
YMP
Joined: 23 Dec 2006 Posts: 418 Location: Russia
|
Posted: Sun Aug 16, 2009 10:57 am Post subject: |
|
|
Global and static variables are both permanent, unlike local ones, but static are visible only to the function that created them. So, use them if you want this limited visibility.
All variables declared outside any function are global. Arrays can be local. |
|
| Back to top |
|
 |
pajenn
Joined: 07 Feb 2009 Posts: 384
|
Posted: Sun Aug 16, 2009 11:23 am Post subject: |
|
|
is there a way to make an array global without making all the variables in the function global? i.e. without using 'global' alone as the first word in the function?
For example, suppose my array is called extraInfo, and my functions performs a loop that sometimes adds entries to it of the form extraInfo%refNumber%:= refInfo... _________________ Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler. |
|
| Back to top |
|
 |
YMP
Joined: 23 Dec 2006 Posts: 418 Location: Russia
|
Posted: Sun Aug 16, 2009 12:31 pm Post subject: |
|
|
Probably not.
| Functions wrote: |
Consequently, a function can create a global array manually (by means such as Array%i% := A_Index) only if it has been defined as an assume-global function. |
|
|
| Back to top |
|
 |
n-l-i-d Guest
|
Posted: Sun Aug 16, 2009 12:49 pm Post subject: |
|
|
| Quote: | | is there a way to make an array global without making all the variables in the function global? i.e. without using 'global' alone as the first word in the function? |
| Code: | SomeFunction()
ListVars
Pause
Return
SomeFunction()
{
Global ; all array values created are global
Local declareTheOther := 1, variablesThat := "you don't want to be global as local"
Loop 10
extraInfo%A_Index% := "Field No. " . A_Index ; will be global
declareTheOther := variablesThat ; will be local
} |
HTH |
|
| Back to top |
|
 |
pajenn
Joined: 07 Feb 2009 Posts: 384
|
Posted: Sun Aug 16, 2009 1:10 pm Post subject: |
|
|
| n-l-i-d wrote: | Global ; all array values created are global
Local declareTheOther := 1, variablesThat := "you don't want to be global as local" |
thanks. that's a clever workaround. _________________ Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler. |
|
| Back to top |
|
 |
Larry
Joined: 03 Mar 2005 Posts: 61
|
Posted: Sun Aug 16, 2009 3:35 pm Post subject: |
|
|
| YMP wrote: | Global and static variables are both permanent, unlike local ones, but static are visible only to the function that created them. So, use them if you want this limited visibility.
All variables declared outside any function are global. Arrays can be local. |
So, the reason for naming Global Variables in a function is so that their
previous values are available to the function when it's called again. Right?
I'm still a little confused by the term "Global".
Does that mean that any Hotkey routine in my script has access to a
Global variable that was created by another Hotkey Routine in my script?
..or..
Does it mean that a Global Variable is available to all Hotkey Routines in
other AutoHotKey scripts that may have been launched either earlier or later?
Thanks. |
|
| Back to top |
|
 |
elisakay Guest
|
Posted: Sun Aug 16, 2009 3:52 pm Post subject: |
|
|
| Larry wrote: | So, the reason for naming Global Variables in a function is so that their
previous values are available to the function when it's called again. Right? | Wrong, the reason to declaring variables global in a function is to allow the function to access variables with the same name to be accessed outside the function - essentionally this makes sure that a variable inside and outside the function are the "same" variable. If not declared global, variables inside functions are private to the function (even if a variable of the same name is used outside the function).
| Larry wrote: | I'm still a little confused by the term "Global".
Does that mean that any Hotkey routine in my script has access to a
Global variable that was created by another Hotkey Routine in my script?
..or..
Does it mean that a Global Variable is available to all Hotkey Routines in
other AutoHotKey scripts that may have been launched either earlier or later? | Subroutines (including Hotkey and Timer) can only access global variabes, again 'local' function variables cannot be seen/accessed outside the function where they are created.
Static variables, are simply local variables that remember their value each time the function is entered. Otherwise 'local' variables are 'empty' each time the function is entered. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Sun Aug 16, 2009 4:22 pm Post subject: |
|
|
@daonlyfreez
| n-l-i-d wrote: | | Code: | SomeFunction()
ListVars
Pause
Return
SomeFunction()
{
Global ; all array values created are global
Local declareTheOther := 1, variablesThat := "you don't want to be global as local"
Loop 10
extraInfo%A_Index% := "Field No. " . A_Index ; will be global
declareTheOther := variablesThat ; will be local
} |
HTH |
Declaring Global is redundant.. It is automatic when Local is used. |
|
| Back to top |
|
 |
pajenn
Joined: 07 Feb 2009 Posts: 384
|
Posted: Sun Aug 16, 2009 4:26 pm Post subject: |
|
|
see if you can spot the difference:
| Code: | ;copy path from explorer address bar and try pasting it with the different hotkey functions
num:=1
#L::
LocalIncrement()
MsgBox % num
Return
LocalIncrement()
{
num++
}
#G::
GlobalIncrement()
msgBox % num
Return
GlobalIncrement()
{
global
num++
} |
_________________ Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler.
Last edited by pajenn on Sun Aug 16, 2009 4:51 pm; edited 1 time in total |
|
| Back to top |
|
 |
YMP
Joined: 23 Dec 2006 Posts: 418 Location: Russia
|
Posted: Sun Aug 16, 2009 4:41 pm Post subject: |
|
|
| Larry wrote: | | Does it mean that a Global Variable is available to all Hotkey Routines in other AutoHotKey scripts that may have been launched either earlier or later? |
No, they exist within one script. |
|
| Back to top |
|
 |
elisakay Guest
|
Posted: Sun Aug 16, 2009 4:51 pm Post subject: |
|
|
| YMP wrote: | | Larry wrote: | | Does it mean that a Global Variable is available to all Hotkey Routines in other AutoHotKey scripts that may have been launched either earlier or later? |
No, they exist within one script. | YMP, Very nice catch, I completely/totally missed that!  |
|
| Back to top |
|
 |
n-l-i-d Guest
|
Posted: Sun Aug 16, 2009 6:58 pm Post subject: |
|
|
| Quote: | | Declaring Global is redundant.. It is automatic when Local is used. |
You're right, assume-global, thanks!  |
|
| Back to top |
|
 |
Larry
Joined: 03 Mar 2005 Posts: 61
|
Posted: Sun Aug 16, 2009 10:59 pm Post subject: Thanks Everyone. Global vars are no longer a mystery to me! |
|
|
| Thanks Everyone. Global vars are no longer a mystery to me! |
|
| Back to top |
|
 |
|