Canucksctr7
Canucksctr7

Reputation: 67

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!

Upvotes: 0

Views: 3865

Answers (2)

Bartosz Wójtowicz
Bartosz Wójtowicz

Reputation: 1401

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();
}

Upvotes: 1

Mark Smit
Mark Smit

Reputation: 574

Almost.. you can get the value trough

 Properties.Settings.Default.["cookiecutter"]

instead of

 Properties.Settings.Default.cookiecutter

Upvotes: 0

Related Questions