Jump to content

How can I save a variable even after the program is exited?

- - - - -

  • Please log in to reply
8 replies to this topic

#1
lucasmeister

lucasmeister

    Newbie

  • Members
  • Pip
  • 2 posts
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

#2
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
you need to save that value somewhere. Like a file, or the windows registry.

#3
X_Programmer

X_Programmer

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
You can save the varible during the Form_Closing event.


I haven't used databases or anything like that yet, but txt files can work as save files.

#4
killcode

killcode

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Lol, you asked this at dreamincode.
Posted Image

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

killcode said:

Lol, you asked this at dreamincode.

You only know because you google'd for an answer to put here. ;)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
killcode

killcode

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Haha! No! don't even try that! lol i didn't google :P
Posted Image

#7
rueleonheart

rueleonheart

    Newbie

  • Members
  • PipPip
  • 14 posts
You can save the value of a variable in a text or xml file. When you reopen the program, it will get those values again from those files.

#8
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
Well windows application? then you can save the values in settings file. Have a walkthrough on how to create a settings file from the coding for fun
http://blogs.msdn.com/b/coding4fun/archive/2009/06/23/9709252.aspx


#9
doktor83

doktor83

    Newbie

  • Members
  • Pip
  • 1 posts
Save it in app.config.
First you click on project name with right button in project explorer,go to properties -> settings.
You make your fields here,and set scope on User.

then in your code u can read the value with :
double theValue = Program_Name.Properties.Settings.Default.thevaluename;

and you can write with :
Program_Name.Properties.Settings.Default.thevaluename = theValue;
Program_Name.Properties.Settings.Default.Save();





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users