 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
lostworld Guest
|
Posted: Wed Feb 24, 2010 12:03 pm Post subject: static variable error |
|
|
hi when i use static in my script it gives error something like below
| Code: |
static a ;;;; error no recognized action
a=1
msgbox %a%
return
|
the purpose for using static variable is when i start the script next time the value should be 2 that is value should be saved at every run but how to achieve this??? |
|
| Back to top |
|
 |
Klaus
Joined: 12 May 2005 Posts: 325 Location: Münster, Germany
|
Posted: Wed Feb 24, 2010 12:15 pm Post subject: |
|
|
Hi, lostworld,
static variables can be used in functions only. If you need to have a continuous content of a variable in your main programme persistent over several programme ends and starts, you have to write the content of the variable to an .INI-file (as one possibility) at programme's exit and re-read/restore it at the next start of your application.
Have a look at IniRead and Iniwrite in the manual.
Hope I could help,
Klaus |
|
| Back to top |
|
 |
lostworld Guest
|
Posted: Wed Feb 24, 2010 12:19 pm Post subject: |
|
|
| thanks buddy will see to it |
|
| Back to top |
|
 |
lostworld Guest
|
Posted: Wed Feb 24, 2010 12:38 pm Post subject: |
|
|
| yep i succeeded...thanks Klaus |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7295 Location: Australia
|
Posted: Wed Feb 24, 2010 12:44 pm Post subject: |
|
|
I like to use this trick:
| Code: | /*
[init]
*/
var=1
var += 1
IniWrite, %var%, %A_ScriptFullPath%, init, var | Of course, it won't work if the script is compiled. If 'var=' is misplaced, IniWrite will haphazardly throw a new one in somewhere. It's usually safer (but less cool since you need to use IniRead) to put the INI section at the end:
| Code: | IniRead, var, %A_ScriptFullPath%, section, var
var += 1
IniWrite, %var%, %A_ScriptFullPath%, section, var
/*
[section]
var=1
|
|
|
| Back to top |
|
 |
Guest
|
Posted: Wed Feb 24, 2010 1:38 pm Post subject: |
|
|
| Lexikos wrote: | | (but less cool since you need to use IniRead) |
...is this cooler?...
| Code: | Gosub, Settings
var++
IniWrite, %var%, %A_ScriptFullPath%, Settings, var
/*
[Settings]
*/
Settings:
var=1
return |
...also a bug/annoyance: I save the script with no line break at the end of the file & the IniWrite adds one next time I run it...why?...(I understand if it's just the WinAPI doing it, but it's still annoying)...
Also, I hate ini files being written when there is no change, so I might need to write an IniWrite() function to only really write if the requested write is different than what's in there already... |
|
| Back to top |
|
 |
lostworld Guest
|
Posted: Thu Feb 25, 2010 6:24 am Post subject: |
|
|
| thanks to all i tried quite similar as Lexikos and its working properly...whenever the file is run the variable has the last incremented value and thats what i was expecting and its working fine so thanks again to all |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|