"Permanently" static variables 
Author Message
 "Permanently" static variables

I need a creative way to create a "permanently" static variable that retains
its value, even in between application executions and exits.

I was thinking obviously making it global and static, and then writing the
value to a notebook. Every time the application is run, it initially reads
the value from a notepad/word document. This is too easy to accidentally
alter the notepad/word document though, and definitely could mess up the
logic. Is there another way? The variable is sort of like an accounting
year. Much of my logic is dependent on the year, and I don't want the year
to change, unless the user finally authorizes it.

Thanks in advance,
David Eynon



Mon, 29 Aug 2005 01:09:05 GMT  
 "Permanently" static variables

Quote:

>I need a creative way to create a "permanently" static variable that retains
>its value, even in between application executions and exits.

>I was thinking obviously making it global and static, and then writing the
>value to a notebook. Every time the application is run, it initially reads
>the value from a notepad/word document. This is too easy to accidentally
>alter the notepad/word document though, and definitely could mess up the
>logic. Is there another way? The variable is sort of like an accounting
>year. Much of my logic is dependent on the year, and I don't want the year
>to change, unless the user finally authorizes it.

>Thanks in advance,
>David Eynon

The common ways to do this is:
1) Use an INI file (IMOHO Recommended)
2) Use the Registry (IMOHO NOT Recommended)

If someone takes it upon them selves to modify an INI file then they null and void any and all warrenties...
I believe that most people are not going to modify the INI file or the Regestry because of fear of crashing the System... ;->

HTH

Have a good day...

Don



Mon, 29 Aug 2005 01:23:33 GMT  
 "Permanently" static variables
Write it to a binary file instead?

or, if your app is using a database,
it should of course go there...

--
Dag.


Quote:
> I need a creative way to create a "permanently" static variable that
retains
> its value, even in between application executions and exits.

> I was thinking obviously making it global and static, and then writing the
> value to a notebook. Every time the application is run, it initially reads
> the value from a notepad/word document. This is too easy to accidentally
> alter the notepad/word document though, and definitely could mess up the
> logic. Is there another way? The variable is sort of like an accounting
> year. Much of my logic is dependent on the year, and I don't want the year
> to change, unless the user finally authorizes it.

> Thanks in advance,
> David Eynon



Mon, 29 Aug 2005 01:25:48 GMT  
 "Permanently" static variables
Although I don't personally like the Windows Registry, your own requirement
is exactly what it is intended for. You can use the various API Registry
methods to read / write to any part of the registry you desire, but the VB
SaveSetting statement and GetSetting function will easily do the job for you
to the area of the registry that they are intended to serve. My own
preferred method, though, would be to write a simple "ini" file into your
application's folder (the VB App.Path property will tell you where this is).
You can use the standard VB File I/O methods to write and read your ini
file. Read the stuff when your app starts up (in the main Form's Load event,
if it has a Form) and write it in the main Form's Unload event. When reading
you should first check that the file actually exists and that it contains
valid information. Most sensible users won't mess about with their app's ini
file, but you can always write some simple "code" and "decode" and "check
sum" stuff if you are worried about them seeing what is there or altering it
in some way. About the only real advantage of using the Registry versus
using your own "home grown" ini file is that most (but not all!) users will
find it much more difficult to mess about with your saved settings. By the
way, I have replied only to the comp.lang.basic.visual.misc group, the only
VB6 group you will ever need ;-)

Mike


Quote:
> I need a creative way to create a "permanently" static variable that
retains
> its value, even in between application executions and exits.

> I was thinking obviously making it global and static, and then writing the
> value to a notebook. Every time the application is run, it initially reads
> the value from a notepad/word document. This is too easy to accidentally
> alter the notepad/word document though, and definitely could mess up the
> logic. Is there another way? The variable is sort of like an accounting
> year. Much of my logic is dependent on the year, and I don't want the year
> to change, unless the user finally authorizes it.

> Thanks in advance,
> David Eynon



Mon, 29 Aug 2005 02:00:49 GMT  
 "Permanently" static variables
In responce to the post:
 On Wed, 12 Mar 2003 17:09:05 GMT, "David Eynon"

Quote:
>I need a creative way to create a "permanently" static variable that retains
>its value, even in between application executions and exits.

>I was thinking obviously making it global and static, and then writing the
>value to a notebook. Every time the application is run, it initially reads
>the value from a notepad/word document. This is too easy to accidentally
>alter the notepad/word document though, and definitely could mess up the
>logic. Is there another way? The variable is sort of like an accounting
>year. Much of my logic is dependent on the year, and I don't want the year
>to change, unless the user finally authorizes it.

>Thanks in advance,
>David Eynon

Take a look at the GetSetting & SaveSetting functions.
Shell
-
http://drshell.home.mindspring.com/
Into computers since 1972.
-


Mon, 29 Aug 2005 03:33:49 GMT  
 "Permanently" static variables
One more solution:  Did you create a database for your application?  If so,
you could easily add a table call "SystemData" or whatever to store
information important to your app.

Bob Walsh.


Quote:
> I need a creative way to create a "permanently" static variable that
retains
> its value, even in between application executions and exits.

> I was thinking obviously making it global and static, and then writing the
> value to a notebook. Every time the application is run, it initially reads
> the value from a notepad/word document. This is too easy to accidentally
> alter the notepad/word document though, and definitely could mess up the
> logic. Is there another way? The variable is sort of like an accounting
> year. Much of my logic is dependent on the year, and I don't want the year
> to change, unless the user finally authorizes it.

> Thanks in advance,
> David Eynon



Mon, 29 Aug 2005 06:03:08 GMT  
 "Permanently" static variables
You can't have a "public static" variable, that is, a variable globally
available with the Static keyword. Static is not necessary in this case ..
just define the variable in a bas mod as Public and assign a value that has
been read from a file or from the registry on program load.

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.


# I need a creative way to create a "permanently" static variable that
retains
# its value, even in between application executions and exits.
#
# I was thinking obviously making it global and static, and then writing the
# value to a notebook. Every time the application is run, it initially reads
# the value from a notepad/word document. This is too easy to accidentally
# alter the notepad/word document though, and definitely could mess up the
# logic. Is there another way? The variable is sort of like an accounting
# year. Much of my logic is dependent on the year, and I don't want the year
# to change, unless the user finally authorizes it.
#
# Thanks in advance,
# David Eynon
#
#



Mon, 29 Aug 2005 09:36:35 GMT  
 "Permanently" static variables
In responce to the post:

stated...and I replied:

Quote:
>You can't have a "public static" variable, that is, a variable globally
>available with the Static keyword. Static is not necessary in this case ..
>just define the variable in a bas mod as Public and assign a value that has
>been read from a file or from the registry on program load.

>--

>Randy Birch
>MVP Visual Basic
>http://www.mvps.org/vbnet/
>Please respond only to the newsgroups so all can benefit.

Yea, what he said...
Shell
-
http://drshell.home.mindspring.com/
Into computers since 1972.
-


Mon, 29 Aug 2005 12:56:29 GMT  
 "Permanently" static variables


Quote:
>. My own
> preferred method, though, would be to write a simple "ini" file into your
> application's folder (the VB App.Path property will tell you where this

is).
I'd go for that too.

Quote:
> You can use the standard VB File I/O methods to write and read your ini
> file.

No need. Windows has API calls for doing these actions. If you've just used
App.Path to generate the fully qualified path to the INI file you can just
pass this to the API and it'll look in the place of your choice instead of
the default windows home directory.

SteveM



Tue, 30 Aug 2005 01:09:48 GMT  
 "Permanently" static variables
On Thu, 13 Mar 2003 17:09:48 -0000, "Stephen Malbon"

<snip>

Quote:
>> You can use the standard VB File I/O methods to write and read your ini
>> file.
>No need. Windows has API calls for doing these actions. If you've just used
>App.Path to generate the fully qualified path to the INI file you can just
>pass this to the API and it'll look in the place of your choice instead of
>the default windows home directory.

>SteveM

I would be a bit wary of the GetPrivateProfile/SetPrivateProfile APIs
- under some circumstances they can actually go to the registry
- also they are spectacularly inefficient

- safer and easier to write one's own INI file manager



Tue, 30 Aug 2005 02:25:06 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Static variables/scope - "Accumulate"?

2. Loop print "VARIABLE", "VARIABLE"

3. static linked "DLLs"

4. Best static "ID" for licensing

5. dynamically dim a variable ie dim "variable"&n

6. *"*-.,._,.-*"* I"LL TRADE VISUAL C++ FOR VBASIC *"*-.,_,.-*"*

7. "Permanently hiding Win95 Task Bar

8. "proper" use of variables in VBA

9. GetObject("","InternetExplorer.Application") fails in Excel VBA

10. Acc97: Problems using "Command" variable

11. SysCmd 603, "path","path"

12. Compile error: "Variable not defined"

 

 
Powered by phpBB® Forum Software