简体   繁体   中英

How do I set a value if a method is called from a child form?

My application currently has 2 forms. It creates a sub form, Form2, which ends with the following code:

public partial class Form2 : Form
{ ...
    Form1 frm = new Form1();
        frm.rglu = glu;
        frm.rdate = fulldate;
        frm.sort();
    Close();
}

Note that form1 is just a couple of buttons at the moment. One starts off Form2 as follows:

    private void button2_Click(object sender, EventArgs e)
    {
        using (Form2 AcqForm = new Form2())
        {
            AcqForm.ShowDialog(this);
        }
    }

No other code runs except a button test(); shown later). This frm.sort(); runs the following code found in Form1:

public partial class Form1 : Form
{
    public void sort() 
    {           
        datelist = new List<DateTime>(rdate);
        datelist.Sort((a, b) => a.CompareTo(b));

        var result = rdate
        .Select((d, i) => new { Date = d, Int = rglu[i] }) 
        .OrderBy(o => o.Date) 
        .ToArray();

        this.rdate = result.Select(o => o.Date).ToArray();
        this.rglu = result.Select(o => o.Int).ToArray();  //all works fine

        for (int i = 7; i+7 <= rglu.Length; i++)
        {
    Console.WriteLine(Convert.ToString(rdate[i]) + Convert.ToString(rglu[i]));
        } //This shows values as expected
    }
}

However when I set a button to run some more code using rglu and rdate the I get null pointer errors:

public partial class Form1 : Form
{ 
    private void test(object sender, EventArgs e)
    {
        for (int i = 7; i < rglu.Length; i++){} //rglu is null! The values are lost.
    }
}

I believe the solution requires a int[] rglu {get; set;} int[] rglu {get; set;} method. However so far I have been unsuccessful in using these things at all. Has anyone encountered this problem?

Edit: rglu is defined like this:

public int[] rglu { get; set; } //I don't get how this works though

Purely from a defensive coding style point of view I think it would be good practice to test for rglu being null before calling methods on it.

eg

public void test() 
{
    if(rglu == null)
    {
        throw new InvalidOperationException("rglu is null!");
    } 

    for (int i = 7; i < rglu.Length; i++){} //rglu is not null!
}

I'd also question why test() needs to be public if it's called from a button click event.

Form1 frm = new Form1();
frm.rglu = glu;
frm.rdate = fulldate;
frm.sort();
Close();

You don't use frm variable anywhere else and don't display the form, so garbage collectior is the only one who is going to receive that data in rdate and rglu variables in this case.

It seems like you wanted to operate on already existing form. In this case you must pass a reference to existing Form1 to your method in Form2 .

Update:

It may look like this:

public partial class Form2 : Form
{
    // ...
    private readonly Form1 _parent;

    public Form2(Form1 parent) : this()
    {
        _parent = parent;
    }

    // ... somewhere in a method which closes Form2:

        Form1 frm = _parent;
        frm.rglu = glu;
        frm.rdate = fulldate;
        frm.sort();
        Close();

    // ...
}

To show Form2 from Form1 , use

using(var form2 = new Form2(this))
{
    form2.ShowDialog(this);
}

Problem is in form2, You mustn't write 'Form1 frm = new Form1();' this code.

in Form1 wite code like this:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public int rglu;
        public DateTime rdate;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnShowForm2_Click(object sender, EventArgs e)
        {
            Form2 frm2=new Form2();
            frm2.Form1Instance = this;
            frm2.Show();
        }
        public void sort()
        {
            datelist = new List<DateTime>(rdate);
            datelist.Sort((a, b) => a.CompareTo(b));

            var result = rdate
            .Select((d, i) => new { Date = d, Int = rglu[i] })
            .OrderBy(o => o.Date) // Sort by whatever field you want
            .ToArray();

            this.rdate = result.Select(o => o.Date).ToArray();
            this.rglu = result.Select(o => o.Int).ToArray();  //all works fine

            for (int i = 7; i + 7 <= rglu.Length; i++)
            {
                Console.WriteLine(Convert.ToString(rdate[i]) + Convert.ToString(rglu[i]));
            } //This returns values as expected
        }
    }
}

in Form2 write codes like this:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
        public Form1 Form1Instance;
        public Form2()
        {
            InitializeComponent();
        }

        private void btnSortValues_Click(object sender, EventArgs e)
        {
            Form1Instance.rglu = glu;
            Form1Instance.rdate = fulldate;
            Form1Instance.sort();
            Close();
        }
    }
}

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