• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

C# Save State of Checkboxes in a Dialog and Retrieve an Int

Jpp

Administrator
So I decided to learn C# a few hours ago and ran into some problems.

I want it to save the state of the checkboxes in the dialog shown in the pictures below. So when I open the dialog up again (without closing Main before) it should remember the exact same states.

HURD4uu.png


In addition, I'm having some troubles getting an Int from the dialog to main. I want main to be able to see Bitmask and know the value of it.
Code:
        public int Bitmask { get; set; }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                Bitmask += 1;
            if (checkBox2.Checked)
                Bitmask += 4;
            if (checkBox3.Checked)
                Bitmask += 8;
            if (checkBox4.Checked)
                Bitmask += 64;
            if (checkBox5.Checked)
                Bitmask += 1024;
            if (checkBox6.Checked)
                Bitmask += 2;
            if (checkBox6.Checked)
                Bitmask += 16;
            if (checkBox6.Checked)
                Bitmask += 32;
            if (checkBox6.Checked)
                Bitmask += 128;
            if (checkBox6.Checked)
                Bitmask += 512;

            this.Close();
        }

I might be using some wrong terms, my coding might be shitty, but keep in mind that I have only been playing around with C# for like 2 hours and this is the second program I'm making.
 
Last edited:

Tommy

Founder
So I decided to learn C# a few hours ago and ran into some problems.

I want it to save the state of the checkboxes in the dialog shown in the pictures below. So when I open the dialog up again (without closing Main before) it should remember the exact same states.

HURD4uu.png


In addition, I'm having some troubles getting an Int from the dialog to main. I want main to be able to see Bitmask and know the value of it.
Code:
        public int Bitmask { get; set; }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                Bitmask += 1;
            if (checkBox2.Checked)
                Bitmask += 4;
            if (checkBox3.Checked)
                Bitmask += 8;
            if (checkBox4.Checked)
                Bitmask += 64;
            if (checkBox5.Checked)
                Bitmask += 1024;
            if (checkBox6.Checked)
                Bitmask += 2;
            if (checkBox6.Checked)
                Bitmask += 16;
            if (checkBox6.Checked)
                Bitmask += 32;
            if (checkBox6.Checked)
                Bitmask += 128;
            if (checkBox6.Checked)
                Bitmask += 512;

            this.Close();
        }

I might be using some wrong terms, my coding might be shitty, but keep in mind that I have only been playing around with C# for like 2 hours and this is the second program I'm making.

You can use the Xml saving system I made if you want it to save like that. I mentioned it in this thread: http://emudevs.com/showthread.php/2504-C-xml-root-element-is-missing?p=17264&viewfull=1#post17264

About the boxes being marked and it incrementing the value, you should decrement the value once they uncheck the box too. :p
 

Jpp

Administrator
Isn't it possible to store 'Bitmask' in RAM? I won't be closing the whole program just the dialog.

About decrementing a value from Bitmask when they uncheck; this was just a test so I didn't really work on that.
 

Parranoia

Insane Member
Isn't it possible to store 'Bitmask' in RAM? I won't be closing the whole program just the dialog.

About decrementing a value from Bitmask when they uncheck; this was just a test so I didn't really work on that.

Have a variable in your main class store the states, such as an array
 
  • Like
Reactions: Jpp

Tommy

Founder
Isn't it possible to store 'Bitmask' in RAM? I won't be closing the whole program just the dialog.

About decrementing a value from Bitmask when they uncheck; this was just a test so I didn't really work on that.

Do you mean like a List or an array of some sort?
 

Jpp

Administrator
Anything would really do, but yeah, it comes down to the same problem. I'm unable to retrieve an int from my Dialog to Main.

This is what I try in the main.
Code:
textBox3.Text = Dialogs.RaceClass.Bitmask.get;

This is the error I get.
Error: An object reference is required for the nonstatic field method or property
 

Tommy

Founder
Anything would really do, but yeah, it comes down to the same problem. I'm unable to retrieve an int from my Dialog to Main.

This is what I try in the main.
Code:
textBox3.Text = Dialogs.RaceClass.Bitmask.get;

This is the error I get.
Error: An object reference is required for the nonstatic field method or property

What is .get? .ToString() will return the integer as a string. That error also occurs when a variable/property is not static, but you're calling the non-static method/property in a static function (not localized). (Error returns more problems, but I pointed out one)
 

Jpp

Administrator
.get is from RaceClass.cs
Code:
public int Bitmask { get; set; }

I might be using it wrong?
 

Tommy

Founder
.get is from RaceClass.cs
Code:
public int Bitmask { get; set; }

I might be using it wrong?

get = returns the value
set = sets the value

For example:

Code:
private int bitmask = 1;

public int Bitmask
{
    get { return bitmask; }
    set { bitmask = value; }
}

You don't use .get and .set as they have already served their purpose by returning/setting the value. Instead, as I pointed out use .ToString:

Code:
textBox3.Text = Dialogs.RaceClass.Bitmask.ToString();

Also, that error could most likely be related to Bitmask not being a static variable because you're trying to call it from RaceClass. However, as it is right now you'd have to do:

Code:
Dialogs.RaceClass raceClass = new Dialogs.RaceClass();

BUT, if you make Bitmask a static variable you won't have to worry about that:

Change:

Code:
public int Bitmask { get; set; }

to:

Code:
public static int Bitmask = 0;
 
Last edited:
  • Like
Reactions: Jpp

Jpp

Administrator
I think I fixed it now. I added this instead.
Code:
public static int Bitmask = 0;

Edit: Didn't see your last post. Either way, thank you for your help, and for the checking/unchecking boxes I could just set Bitmask to 0 when I open the dialog.

Thank you for your time.
 

Tommy

Founder
You wouldnt have to worry about decrementing if you only did = not +=

When you increment Classmasks or Racemasks the values can pertain to multiple classes/races. If he didn't increment then it would only pertain to only a specific race/class. This goes for flags too.
 
Top