简体   繁体   中英

How to Save a Boolean Value in C#

I am making a game similar to "Make it Rain" or "Cookie Clicker." I have settings where you can purchase items to make each click of the cookie produce more cookies. I have been trying to make the game save the variable. For example, each click produces a cookie. If you have 100 cookies, you can buy a cookie cutter, so each click produces 5 cookies, and so on. I have booleans called cookiecutters, which are false. If the user buys the item, cookiecutters = true. I would like to save this variable to true so when the user closes and reopens the form, each click produces 5 cookies. This is what I have come up with, yet it doesn't work.

private void Form1_Load(object sender, EventArgs e)
{
    if (cookiecutter == true)
    {
        cookiecutter = Convert.ToBoolean(Properties.Settings.Default.cookiecutter);
        cookiecutter = Properties.Settings.Default.cookiecutter;
    }
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    if (cookiecutter== true)
    {
        Properties.Settings.Default.cookiecutter = cookiecutter = true;
    }

    Properties.Settings.Default.Save();
}

Thank you all for the help in advance!

Your code looks weird. Try to make these changes and make sure your initial value in the settings for cookiecutter is false.

private void Form1_Load(object sender, EventArgs e)
{
    cookiecutter = Convert.ToBoolean(Properties.Settings.Default.cookiecutter);
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    Properties.Settings.Default.cookiecutter = cookiecutter;
    Properties.Settings.Default.Save();
}

Almost.. you can get the value trough

 Properties.Settings.Default.["cookiecutter"]

instead of

 Properties.Settings.Default.cookiecutter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM