C
C
CryptoPython2019-03-15 00:15:44
OOP
CryptoPython, 2019-03-15 00:15:44

Why is the object created in the class destroyed?

In general, I am writing in the form of c # and I ran into the problem of the program crashing, now I will try to explain.
in the main class of the form I create a link to an object on another form, later I fill in the datagrid in this other form with data from the first

Result r1 = new Result();

public void test()
{
    for (int i = 0; i < Convert.ToInt32(timeCreditText.Text.ToString()); i++)
    {
        try
        {
            r1.dataGridView1.Rows.Add();
            r1.dataGridView1.Rows[i].Cells[0].Value = (i + 1).ToString();
        }
        catch(Exception e)
        {
            MessageBox.Show("error");
        }
    }
    r1.Show();
}

and the problem is that when I close window 2 of the form and click again to open it and fill it with data, I get an exception that System.ObjectDisposedException: "Access to the disposed object is not possible."
I hide that other form like this: MainForm.r1.Hide(); and it is not clear why the reference to the object is destroyed.
BUT if you create a new object each time you click, then everything is OK, and I used this, but it’s not clear why the created object of another form in the main class of the main form is destroyed when that other form is hidden.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question