CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2010
    Posts
    3

    Smile 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

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    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.

  3. #3
    Join Date
    Dec 2009
    Posts
    596

    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 methodsatabase and the registry but I believe the file method is your ticket.

  4. #4
    Join Date
    Dec 2009
    Posts
    596

    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

  5. #5
    Join Date
    May 2010
    Posts
    3

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

    Quote Originally Posted by viperbyte View Post
    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 methodsatabase 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

  6. #6
    Join Date
    Dec 2009
    Posts
    596

    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.

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  8. #8
    Join Date
    Feb 2007
    Posts
    43

    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
  •  





Click Here to Expand Forum to Full Width

Featured