将函数传递给构造函数

时间:2014-01-14 06:18:37

标签: c# methods constructor

如果我需要传递给构造函数的变量很少,但不想单独传递它们,而不是通过函数/方法将它们传递给构造函数。可能吗?你能带一个语法的例子吗?

e.g。

string name_edit;
string surname_edit;
string phone_edit;
string email_edit;

private void EditContact()
{
    EditedContactDetails();
    SecondaryForm EditContactForm = new SecondaryForm(false, name_edit, surname_edit, phone_edit, email_edit);
    EditContactForm.testForm = this;
    if (EditContactForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        fillTheList();
    }
}

我想传递name_edit, surname_edit, phone_edit, email_edit,例如:

prWindow(Contact);

1 个答案:

答案 0 :(得分:2)

把它们放在一个班级中:

class Contact
{
   public string name_edit {get; set;}
   public string surname_edit {get; set;}
   public string phone_edit {get; set;}
   public string email_edit {get; set;}
}

并修改SecondaryForm的构造函数以接受Contact参数:

public SecondaryForm (Contact contact)
{
   ...
}
相关问题