All respect for great PhiLho, but I think you're wrong this time
.
Hey, great men make errors sometime, even more when doing stuff in haste. ;-)
Static variables are not empty in the Timer label (can check with my Testcode in first post). I use this many times to avoid global variables.
Ouch, somehow I missed this info, my message boxes are small with my theme, so the title is truncated.
And indeed, since variables are static, there is no reason why they should go out.
I see you added to your post, but with an error
, sorry. local VarToMake gives an error, because all variables in a function are local.
I added this as an afterthought, because I take the habit to always declare local my local variables, even if it is not necessary. Thus, I don't have to think twice to be sure to reach global variables.
I don't understand this message:
---------------------------
__Test.ahk
---------------------------
Error at line 50.
Line Text: local VarToMake
Error: Local variables do not need to be declared in this function.
The program will exit.
---------------------------
OK
---------------------------
I re-read the Functions page of the the manual and I don't see such restriction. It shows that only because the other variables are static, ie. if I declare them local, it is OK...
Aha, it works if I declare the local variable first! It is nearly a bug, at least, an obscure feature...
Within a function, any dynamic variable reference such as Array%i% will always resolve to a local variable [...]If neither exists and the usage requires the variable to be created, it will be created as a local variable.
No simple mean to create it as
static variable.
It can be a bug, where AutoHotkey look in the lists of local and global variables, but not in the list of static variables?
Uhu, the following code doesn't work either, so either I am missing something, or you really have put the finger on a bug.
NewVar()
F9::ExitApp
NewVar()
{
local VarToMake, VarTest
static TestVar, Index
If TestVar = ; First init of static vars
{
Index= Test
TestVar= {esc}
}
VarToMake:= %Index%Var ; -> content of VarToMake is {esc}
MsgBox TestVar= %TestVar%`nIndex=%Index%`n1: VarToMake= %VarToMake%
SetTimer, TimerLabel, 1000
Return
TimerLabel:
SetTimer, TimerLabel, off
VarTest := TestVar
VarToMake := Var%Index%
MsgBox TestVar= %TestVar%`nVarTest= %VarTest%`nIndex=%Index%`n2: VarToMake= %VarToMake%
Return
}