Hello,

I am trying to pass some information to a new page, create a string array, and then use that information in additional code in the application. It seems to work fine until the last step, where the array values seem to disappear (goes null). Somewhere between when the new window closes and I am returned to the original code, I lose the information. I feel this is because the page closes and destroys the instance, so what remains is this: how do I pass the collected information from the second page to the first so that it remains there after I close the second page. I have detailed the process in the code below. Please let me know where I am making my mistake. Thank you in advance for your help, Pat

//Constructor from the Where page
        public frmMain(ArrayList isChecked)
        {
            InitializeComponent();

            this.checkedNames = isChecked; //This works perfect to here.
            //The code then returns to close the window and then comes back
            //to the scipt below
        }

//******* This is where the code starts *********


private void buttonWhere_Click(object sender, EventArgs e)
        {
            for (int iItem = 0; iItem <= listViewColumns.Items.Count - 1; iItem++)
            {
                colNames.Add(listViewColumns.Items[iItem].SubItems[0].Text);
            }

            //Send the column names to the new window page for selection in a check list box
            Where w = new Where(colNames); 
            w.ShowDialog();
        
            //Page then goes to the windows page and the returns to the constuctor where the values 
            //have transfered correctly and propogated the Arraylist checkedNames correctly!  The action then 
            //returns to the window page to close it and then comes back to this point. 

            //Return from the window page starts here
            foreach (string name in checkedNames)
            {
                //At this point, the checkedNames Code has turned to null.  It seems to have happened after the 
                //window page was closed.  If that is the case, how do I get the values from that to this page
                //so I can use the selected values in the code!!
            }
        }

Please ensure that an reference of an instance of ArrayList is not null.

Code to open a new window

ArrayList ar=new ArraList();
 frmMain frm=new frmMain(ar);
 frm.ShowDialog();
 ....

Hello, thank you for the reply. No, that is not it. On line 22 above, I pass an array of info to a new page called Where. Using that info, I create a NEW array of info called ischecked (line 2 above). The information is accepted back on the first page correctly in the constructor...so far so good...BUT, as soon as I close the page "Where", the info is dissolved. I tried making a copy of it and placing it in a new array (checkedNames), but since the copy actually refers to the memory location (by reference), it goes away with the page, as does the copy. So...I guess my real question is how can I copy this information and keep it so that when the page is closed, the information is retained? I have read about something called a "deep copy" but the code is not clear to me. I have tried all the array copy functions I can find, but none work any better....any ideas? Thanks for the help...Pat

I guess my real question is how can I copy this information and keep it so that when the page is closed, the information is retained?

Classes are reference types. Assignment to a variable of reference type creates a copy of the reference but not of the referenced object.

In constructor method, reference of an object will be copied.

public frmMain(ArrayList isChecked) {... }

You could use Cloning of objects by implementing ICloneable interface.

Have a look at this method.

Clone method of ArrayList (Collection of immutable strings)

ArrayList a = new ArrayList();
a.Add("A");
a.Add("B");

ArrayList b =(ArrayList)a.Clone();
b.Add("44");

MessageBox.Show(a.Count.ToString());

Clone method of ArrayList (Collection of an array of immutable strings)

ArrayList a = new ArrayList();
 a.Add(new string[] { "A", "B" });
 a.Add(new string[] { "P", "Q" });
 ArrayList b = (ArrayList)a.Clone(); // copy of an ArrayList object but not string[]

 ((string[])b[0])[0] = "Test";

 MessageBox.Show(((string[])a[0])[0]);  // Will print "Test"

Example to create a copy of List as well as strings[]

class MyArray : List<string[]>, ICloneable 
{
    public object Clone()
    {
        MyArray temp = new MyArray();
        foreach (string[] ar in this)
        {
            string[] newar = new string[ar.Length];
            //Copy an array
            ar.CopyTo(newar, 0);

            temp.Add(ar);
        }
        return temp;
    }
     
}
....
....
....
 MyArray a = new MyArray();
 a.Add(new string[] { "A", "B" });
 a.Add(new string[] { "P", "Q" });

 MyArray b =(MyArray) a.Clone();
 b[0][0] = "Test";
 MessageBox.Show(a[0][0]);

Edited 6 Years Ago by __avd: n/a

Yes, now I get it. I also now understand your first post. Thank you for the very detailed explanation. I not only have solved the problem, but I also have a better understanding of Arrays, the most troublesome issue (for me) in coding. To resolve the problem, I created a new array at the showDialog point and referenced it to the array on the second page. Now, it remains active after the page is destroyed. Many thanks, I have marked this issue as resolved. Best regards, Pat

This question has already been answered. Start a new discussion instead.