Mike
Mike

Reputation: 533

Pass a function to a constructor

If I have few variables which I need to pass to a constructor, but do not want to pass them separately, instead of passing them via a function/method to a constructor. Is it possible? Could you bring an example of syntax?

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();
    }
}

I want to pass name_edit, surname_edit, phone_edit, email_edit for example like this:

prWindow(Contact);

Upvotes: 0

Views: 298

Answers (1)

Alberto
Alberto

Reputation: 15951

Put them in a class:

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

and modify the constructor of SecondaryForm in order to accept a Contact parameter:

public SecondaryForm (Contact contact)
{
   ...
}

Upvotes: 2

Related Questions