AutoHotkey Community

It is currently May 27th, 2012, 4:23 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: static variable error
PostPosted: February 24th, 2010, 1:03 pm 
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???


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 24th, 2010, 1:15 pm 
Offline

Joined: May 12th, 2005, 8:20 am
Posts: 331
Location: Münster, Germany
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 24th, 2010, 1:19 pm 
thanks buddy will see to it


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 24th, 2010, 1:38 pm 
yep i succeeded...thanks Klaus


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 24th, 2010, 1:44 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 24th, 2010, 2:38 pm 
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...


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 25th, 2010, 7:24 am 
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


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Mickers, rbrtryn, Yahoo [Bot] and 63 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group