0

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

Kiran Joshi
  • 1,758
  • 2
  • 11
  • 34
Neil White
  • 111
  • 4
  • 18
  • I don't understand the issue. Just call the object's constructor? `var p = new person() { Name = name, age = age }`. What is the issue? –  Jul 05 '18 at 13:02
  • Requirement is little bit unclear, can you please more specific? just wanted to create the instance of person in form2 or need to pass the details back to form1? – sujith karivelil Jul 05 '18 at 13:03
  • Start with [naming conventions](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/capitalization-conventions). – Sinatr Jul 05 '18 at 13:03
  • Possible duplicate of [Send values from one form to another form](https://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form) – Sinatr Jul 05 '18 at 13:04
  • Yeah @Amy was write. Was getting myself confused! – Neil White Jul 05 '18 at 13:20

2 Answers2

1

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
};
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
1

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
    }
}
Antoine V
  • 6,998
  • 2
  • 11
  • 34