Add global string to checkedlistbox?

72 views Asked by At

I'm trying to make a to do list using a checkedlistbox, by using a secondary form to set a global string and add that to my checkedlistbox. Problem is it's not working. Heres the code for the add button on my form1 which opens up a secondary form where i set my global string:

private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form2 f2 = new Form2();
            f2.ShowDialog();
            checkedListBox1.Items.Add(global.Items);
        }

Note: global.Items is the global string which i want to add. i dont mind not using a global string, any way to do this is appreciated. thanks in advance

Heres the class where i set Items:

 class global
    {
        public static string Items= "";
    }

And just incase heres the Form2 code:

private void button1_Click(object sender, EventArgs e)
{
    global.Items = textBox1.Text;
    this.Hide();
    Form1 f1 = new Form1();
    f1.ShowDialog();
}
1

There are 1 answers

0
user35443 On

You are not using the global variable global.Items, you're just retrieving its value. If you wanted to use the variable instead, parameter would have to be defined as out or ref, which isn't the case of Items.Add().

What happens is, that the value of global.Items is passed to Items.Add() in the moment when the call is made, but future modifications of global.Items don't affect the item.