简体   繁体   中英

c# winforms array items gets deleted

I know this might look silly but I got a strange problem in my winforms . I have a windows application in which after a particular set of operations are completed I want to populate a Checked ComboBox . I am doing this using two classes. I want to copy a array from helper class to the form class . Array gets copied when AddArrayItems method is called. But when I see the ComboBox in the form, its null. After debugging with watch variables I got to know that the problem is after copying the array to Form1 array, as soon the control goes back to the caller, the array items are deleted. I tried to replicate my stuff, not exactly but still similar to what I am doing.

My code looks like this:

using System;
using System.Windows.Forms;

namespace DemoApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }    
        string[] cboxAr;    

        public void AddCmboBoxItems(string[] cbArry)
        {
            cboxAr = new string[cbArry.Length];
            Array.Copy(cbArry, 0, cboxAr, 0, cbArry.Length); 
            //cbArry.CopyTo(cboxAr, 0);
            //foreach (string s in cboxAr)
                //comboBox1.Items.Add(s);
            comboBox1.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HelperClass.DoSomething();
        }
    }

    public class HelperClass
    {
        public HelperClass()
        {
        }

        public void HelperMethod()
        {
            SomeMethod();
        }

        private void SomeMethod()
        {
            string[] partnrName = new string[5] { "str1", "str2", "str3", "str4", "str5"};

            Form1 f = new Form1();
            f.AddCmboBoxItems(partnrName);
        }

        public static void DoSomething()
        {
            new HelperClass().HelperMethod();
        }
    }
}

I don't understand what exactly the problem is here. Can anyone please push me in the right direction. Thanks in advance.

You're never showing the form after modifying its controls:

Form1 f = new Form1();
f.AddCmboBoxItems(partnrName);

But you're calling this from within an existing form:

private void button1_Click(object sender, EventArgs e)
{
    HelperClass.DoSomething();
}

Presumably you want to modify the controls on that form? Then you'll need a reference to that form. Pass one to the method:

private void button1_Click(object sender, EventArgs e)
{
    HelperClass.DoSomething(this);
}

And accept it in the method definition:

public static void DoSomething(Form1 form)
{
    new HelperClass().HelperMethod(form);
}

And so until the point where you need to use it. (Side note: You have a lot of weird indirection happening here with a seemingly random mix of static and instance methods and classes. You can simplify a lot, which will make this involve fewer code changes.)

Ultimately, SomeMethod needs the instance of the form to modify:

private void SomeMethod(Form1 form)
{
    string[] partnrName = new string[5] { "str1", "str2", "str3", "str4", "str5"};

     form.AddCmboBoxItems(partnrName);
}

To illustrate the overall point, consider an analogy...

A car rolls off of an assembly line. You open the trunk and put a suitcase inside. Moments later another car rolls off of the same assembly line. It is identical to the first car in every way. When you open the trunk of the second car, do you expect to find your suitcase inside it?

A Form is an object like any other. Changes made to one instance of an object are not reflected in other instances of the same object. Each instance maintains its own state. In order to modify a particular instance, you need a reference to that instance.

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