C# 如何将大量参数传递给构造函数?

C# 如何将大量参数传递给构造函数?,c#,.net,winforms,C#,.net,Winforms,我有一个名为“add.cs”的主表单,其中包含用户信息(姓名、年龄等)。在此表单上,我拖动一个按钮添加用户的子女信息(姓名、血型、出生日期等),其单击事件将打开一个表单“addchild.cs”我在上面拖动了两个按钮,一个用于扫描数据,另一个用于在运行时添加控件。为了在运行时添加控件,我使用了以下代码 private void button1_Click(object sender, EventArgs e) { try {

我有一个名为“add.cs”的主表单,其中包含用户信息(姓名、年龄等)。在此表单上,我拖动一个按钮添加用户的子女信息(姓名、血型、出生日期等),其单击事件将打开一个表单“addchild.cs”我在上面拖动了两个按钮,一个用于扫描数据,另一个用于在运行时添加控件。为了在运行时添加控件,我使用了以下代码

 private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                childrenCount++;

                Label lblchild = new Label();
                lblchild.BorderStyle = BorderStyle.Fixed3D;
                lblchild.Location = new Point(20, (childrenCount - 1) * 50);
                lblchild.Size = new System.Drawing.Size(50, 20);
                lblchild.Text = "Child  " + (childrenCount - 1);
                TextBox tName = new TextBox(); //TextBox for the name
                tName.BorderStyle = BorderStyle.Fixed3D;
                tName.Location = new Point(120, (childrenCount - 1) * 50);
                tName.Size = new System.Drawing.Size(135, 60);
                tName.Text = "";
                DateTimePicker calendar = new DateTimePicker();
                //MonthCalendar calendar = new MonthCalendar(); //Calendar to choose the birth date
                calendar.Location = new Point(310, (childrenCount - 1) * 50);
                ComboBox bloodGroup = new ComboBox(); //ComboBox for the blood group
                bloodGroup.Location = new Point(650, (childrenCount - 1) * 50);
                bloodGroup.Size = new System.Drawing.Size(135, 60);
                for (Enum.Blood_Group l = Enum.Blood_Group.O_negative; l <= Enum.Blood_Group.AB_positive; l++)
                {
                    bloodGroup.Items.Add(l);
                }

                this.panel1.Controls.Add(lblchild);
                this.panel1.Controls.Add(tName);
                this.panel1.Controls.Add(calendar);
                this.panel1.Controls.Add(bloodGroup);


            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
然后,在addchild.cs上的submission按钮的click事件中,我使用此代码将控件数据存储到名为Childdata.cs的外部类的变量中

  private void submitbtn_Click(object sender, EventArgs e)
        {
            //Checks the values
            try
            {
                string message = "";
                foreach (Control c in this.Controls)
                { //loop throught the elements of the form
                    if (c.GetType() == typeof(Panel))
                    { //check if the control is a Panel
                        //Get the values from the input fields and add it to the message string
                        Panel panel1 = (Panel)c;
                        Label lblchild = (Label)(c.Controls[0]);
                        TextBox tName = (TextBox)(c.Controls[1]);
                        DateTimePicker calendar = (DateTimePicker)(c.Controls[2]);
                        ComboBox bloodGroup = (ComboBox)(c.Controls[3]);

                        //add to class
                        Childrendata childdata=new Childrendata();
                        childdata.Childname = tName.Text;
                        childdata.Childblood= bloodGroup.Text;
                        childdata.Childbirth=calendar.Text;


                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
         }
现在,我如何使构造函数以数组或任何其他东西作为参数,并存储所有值,我可以调用此构造函数到我的主页add.cs。因此,单击add.cs上按钮的事件,我可以使用如下代码将这些值存储到数据库中

Childrendata cd = new Childremdata();
Children child = new Children();

                    child.Namechild = cd.Childname;
                    child.Bloodchild = cd.Childblood;
                    child.Dobchild = cd.Childbirth;
                    if (new InsertAction().Insertchildren(child))
                    {
                        MessageBox.Show(" Children Insertion Happen ");
                    }
                    else
                    {
                        MessageBox.Show(" Children Insertion does not Happen ");
                    }
请告诉我确切的例子,因为我已经尝试了很多来解决这个问题,但没有结果!!
请帮帮我,请帮帮我,我不确定你到底想要什么,但你可以在Childrendata类中使用如下构造函数:

public class Childrendata
{
   public Childrendata(Children child)
   {
      childname = child.Name;
      //etc...

我建议您创建一个名为“Model”的类,该类将包含您孩子的数据(如姓名、血型等)

这个模型类将成为所有数据的“主”,您可以将它传递给任何其他需要它的表单或类——因此,现在只传递1个参数

当您有多个子对象时,您可以将您的ChildModels放入集合/数组/列表/或ObservableCollection(用于数据绑定)。然后,您可以遍历列表中的每个子项来操作它们

如果要进行下一步,请在模型上实现System.ComponentModel.INotifyPropertyChanged,并使用数据绑定使数据在表单控件中可见,而不是手动将数据移入和移出控件


有关详细信息,请搜索模型视图控制器模式。

您可以查看以下代码:

添加.cs

public class Add
    {
        private List<Childrendata> children;   //Create this List of Childrendata in Add.cs form

        public void CreateChildControls()   // This is on Button click to create another from with controls
        {
            children = new List<Childrendata>(); // Call Add.cs form and pass the List
            AddChild obj = new AddChild(children);
            obj.StoreData();
        }

        public void StoreInDB()  // This is on final Submit to DB
        {
            if (new InsertAction().Insertchildren(children)) // Pass the same List of Childrendata to Save in DB
            {
                MessageBox.Show(" Children Insertion Happen ");
            }
            else
            {
                MessageBox.Show(" Children Insertion does not Happen ");
            }
        }
    }
public class AddChild
{
    private List<Childrendata> m_children;
    public AddChild(List<Childrendata> children)
    {
        this.m_children = children; //Initialize the same List as sent by Mainform
    }

    public void AddControls()
    {

    }

    public void StoreData()  // This is on Submit button click in child form to add new child details
    {
        for (int i = 0; i < 10; i++)
        {
            // Filled with some dummy data
            Childrendata childdata = new Childrendata();
            childdata.Childname = "skumar" + i;
            childdata.Childblood = "B" + i;
            childdata.Childbirth = "2013/10/"+ i;

            m_children.Add(childdata);   // Add to same List
        }
    }
}
   public class Childrendata
    {
        string childname, childblood, childbirth;

        public string Childbirth
        {
            get { return childbirth; }
            set { childbirth = value; }
        }

        public string Childblood
        {
            get { return childblood; }
            set { childblood = value; }
        }

        public string Childname
        {
            get { return childname; }
            set { childname = value; }
        }

    }

澄清你的问题。你想使用列表或数组之类的东西来实例化你的类吗?不,实际上我想将子窗体中所有控件的值存储到父窗体中。请给我一个例子,我可以这样做。我创建的名为“Childdata”的类之间有什么区别包含所有数据和模型类,请告诉我你的ChildData类是一个不错的模型示例——你的思路绝对正确。再读一遍你原来的帖子,我觉得你看起来不错。我建议大家多读一点关于数据绑定的知识。数据绑定将使您不必编写将数据移入和移出表单控件所涉及的非常繁琐的代码。这里有一个关于Winforms数据绑定的教程:不,我不想要这个。实际上,我想在form类中创建一个构造函数,这样我就可以在其中存储所有控件的值,并在父窗体中调用它。我反对这个想法,因为它开始遵循我更喜欢的模型/视图/演示器模型。它允许用户独立地对表单和数据进行单元测试。此外,通过这种方式,在检查输入的数据时,它给了您更多的自由。最初设置可能需要更多的时间,这将为您节省将来的时间。谢谢,先生,先生,请您告诉我,我可以像您上面所说的那样在方法中删除一个方法,以便在按钮的函数中创建一个函数吗click@user2214972:无法在其他方法中创建方法。我的意思是创建一个单独的方法,并从您的按钮单击方法调用它。先生,我已经应用了这个方法,但这个方法在add.cs表单中返回空值,甚至只存储运行时添加的第一个子项的值,而不是用于all@VJain:如果没有完整的代码,很难找到问题所在。我宁愿建议你可以上传到一些免费的文件共享网站,这样我就可以下载和验证它。
   public class Childrendata
    {
        string childname, childblood, childbirth;

        public string Childbirth
        {
            get { return childbirth; }
            set { childbirth = value; }
        }

        public string Childblood
        {
            get { return childblood; }
            set { childblood = value; }
        }

        public string Childname
        {
            get { return childname; }
            set { childname = value; }
        }

    }