简体   繁体   English

C#从另一种形式创建类的新实例

[英]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. 我对使用C#进行面向对象的编程完全陌生,并且想知道使用第二种形式输入细节的最佳方法是什么,该细节用于创建第一种形式上存在的对象的新实例。
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 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 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, 在Form2类中,

First include namespace where the Person class is present Then using new keyword you can create instance of person class 首先包括存在Person类的名称空间,然后使用new关键字创建person类的实例

Person personObj = new Person();

If you want to assign values to the properties present in Person class, then 如果要为Person类中存在的属性分配值,则

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

You can create an object in Form2 and get it in Form1 like this: 您可以在Form2中创建一个对象,然后像这样在Form1中获取它:

    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: 或者,如果要将对象从Form1传递到Form2,在Form2中对其进行更新并重新获取Form1,则可以执行以下操作:

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将值从一种形式传递到另一种形式,而无需在C#中创建形式的新实例? - how to pass a values from one form to another form without creating new instance of a form in c#? 如何返回父表单而不在C#Windows应用程序中创建父类的新实例 - How to come back to parent form without creating new instance of parent class in C# windows application C#根据类型创建类的实例 - C# Creating an instance of a class from type C#:在不定义新类的情况下创建抽象类的实例 - C#: Creating an instance of an abstract class without defining new class C#在另一个类中创建一个类的实例 - C# creating instance of a class in side another class 如何从另一个类访问表单的方法以确保它是相同的Form实例-C# - How to access a method of a form from another class ensuring it is the same Form instance - C# C#将类放入字典中并在调用时创建新实例 - C# Putting a class in a dictionary and creating a new instance on call 创建类的新实例时出现C#StackOverFlowException - C# StackOverFlowException when creating new instance of class 从类访问表单方法而无需创建新实例 - Accessing form method from a class without creating a new instance C#以另一种形式修改类的实例变量 - C# modifying instance variables of a class in another form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM