Home > Back-end >  How to assign a value to class member from another form in C#?
How to assign a value to class member from another form in C#?

Time:07-21

I have 2 form .First one is Form1 and the second is Form2. And also i have a class in Form1 which name is Matrix.

public class Matris
    {
        public int city;
       public int country;


    };

I want to reach the class member from Form2 and assign a value them. But when i create an object from form1 and try to reach them i cant see the Matris member. im creating an object in form2 like this. But i cant see the class or members. Form1 f1=new Form1();

How can i do that? is it possible, thanks for your help.

CodePudding user response:

You need to define property of field of Matris in Form class and then you can use this variable in Form objects:

public class Form
{
    public Matris Matris { get; set; }
}

public class Matris
{
    public int city;
    public int country;
}

And you can use:

Form form = new Form() { Matris = new Matris { City = 1 } };
Form form2 = new Form() { Matris = new Matris { City = 2 } };

or if you want to assign form.Matris to form2.Matris:

form2.Matris = form.Matris;

As a good practice, it is better to separate class in a separate files.

  • Related