|
-
August 11th, 2010, 07:01 AM
#1
How can I save a variable even after the program is exited?
Hi, basically all I want to do is code my project so that it stores any saved variables for the next time the program is run. At the moment once I close and reopen the program it obviously resets any variables. Is there any simple way to do this (please bear in mind, im very new at programming)
Il give you an example. If I have a simple program that when I press a button it adds 1 to a counter. Now next time I open that program and press the button again I want to add to the previous counter.
Thanks in advance
-
August 11th, 2010, 07:34 AM
#2
Re: How can I save a variable even after the program is exited?
You will have to either create a file, save information to a database or save information to the registry. That being said, please don't use the registry unless you really need to. If it is just basic info for your application, create a file and save the variables in there. Search the forum and you will find numerous examples.
-
August 11th, 2010, 07:37 AM
#3
Re: How can I save a variable even after the program is exited?
The most simple approach would be to save your variable values in a file. When your program starts it would open the file and your variables will be initialized with the file's contents. There are other methods atabase and the registry but I believe the file method is your ticket.
-
August 11th, 2010, 07:39 AM
#4
Re: How can I save a variable even after the program is exited?
I didn't realize that sotoasty was writing when I was. So that explains the double similar response
-
August 11th, 2010, 07:42 AM
#5
Re: How can I save a variable even after the program is exited?
 Originally Posted by viperbyte
The most simple approach would be to save your variable values in a file. When your program starts it would open the file and your variables will be initialized with the file's contents. There are other methods  atabase and the registry but I believe the file method is your ticket.
Thanks for your replies. This sounds the most simple way.
Could tell me how to code my program so it opens the file with the variables and saves the file with the variables upon exit? Or direct me to somewhere that explains it?
That would be greatly appreciated 
Thanks
-
August 11th, 2010, 08:00 AM
#6
Re: How can I save a variable even after the program is exited?
I don't know what your intentions are for future programming. If you're going to be a programmer or just take classes you would benifit most by trying to code this. You can search you Visual Studio Help if you have VS or search the internet for the basic snippets. The snippets would be how to open a file, how to read a file, how to save to a file(in C#). The examples might hard code the values that will be written. you replace that with your variables. But if you are not going to be a programmer or take classes on programming and this is just a one time, then I can understand why you won't want to be bothered with all the coding stuff. I'd share some code with you if i had it. But i don't. Sorry.
-
August 12th, 2010, 09:18 AM
#7
Re: How can I save a variable even after the program is exited?
Look at the streamreader and streamwriter classes contained in the system.io
Always use [code][/code] tags when posting code.
-
August 12th, 2010, 11:40 PM
#8
Re: How can I save a variable even after the program is exited?
For stuff where you don't know where to start, google ftw.
I also recommend getting a good beginner's book. EVERY c sharp book will tell you how variables work, and how to save information to a file.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|