简体   繁体   中英

C# Creating new instance of class from another form

I'm completely new to object orientated programming in C# and was wondering what the best way of using a 2nd form to enter details that are used to create a new instance of an object that exists on the first form.
Do I just pass the variables back to the form and create the new instance on the new form. Just wondering the best way....

Basic code for form1

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.ShowDialog();
    }
}

class person
{
    public string Name { get; set; }
    public int age { get; set; }
}

Basic code for Form2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // How do I create a new instance of person using these variables
        string name = "Neil";
        int age = 42;
        this.Close();
    }
}

Any help greatly Appreciated

In Form2 class,

First include namespace where the Person class is present Then using new keyword you can create instance of person class

Person personObj = new Person();

If you want to assign values to the properties present in Person class, then

Person personObj = new Person()
{
    Name = "Nail",
    Age = 23
};

You can create an object in Form2 and get it in Form1 like this:

    public partial class Form1 : Form
    {            

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            if (frm2.ShowDialog() == DialogResult.OK)
            {
                var p = frm2.Person;
            }
        }
   }


   public partial class Form2 : Form
   {
        public person Person { get; set; }            

        private void button1_Click(object sender, EventArgs e)
        {
            this.Person = new Person();
            //set properties in Person object
        }
    }

Or if you want to pass object from Form1 to Form2, update it in Form2 and retake it Form1, you can do like this:

public partial class Form1 : Form
{
    private void button1_Form1_Click(object sender, EventArgs e)
    {
        var p = new Person();
        Form2 frm2 = new Form2(p);
        if (frm2.ShowDialog() == DialogResult.OK)
        {
            var updatedPerson = frm2.Person;
        }
    }
}

public partial class Form2 : Form
{
    public person Person { get; set; }
    public Form2(Person p)
    {
        this.Person = p;
        InitializeComponent();
    }
    private void button1_Form2_Click(object sender, EventArgs e)
    {
        //set properties of this.Person
    }
}

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