AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

static variable error

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
lostworld
Guest





PostPosted: Wed Feb 24, 2010 12:03 pm    Post subject: static variable error Reply with quote

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

PostPosted: Wed Feb 24, 2010 12:15 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
lostworld
Guest





PostPosted: Wed Feb 24, 2010 12:19 pm    Post subject: Reply with quote

thanks buddy will see to it
Back to top
lostworld
Guest





PostPosted: Wed Feb 24, 2010 12:38 pm    Post subject: Reply with quote

yep i succeeded...thanks Klaus
Back to top
Lexikos



Joined: 17 Oct 2006
Posts: 7295
Location: Australia

PostPosted: Wed Feb 24, 2010 12:44 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Guest






PostPosted: Wed Feb 24, 2010 1:38 pm    Post subject: Reply with quote

Lexikos wrote:
(but less cool since you need to use IniRead)

...is this cooler?...

cannibal.ahk wrote:
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





PostPosted: Thu Feb 25, 2010 6:24 am    Post subject: Reply with quote

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
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group