Pass values from one Object to another form C#

Asked

Viewed 2,666 times

2

I have a system, where after the user logs in, I pull a lot of XML data on it and save it in attributes of an object, but there is another form to make the password change ,where I need the User_id of the first form... How do I pass values from one object to another form in c#? Thanks

2 answers

2


Hello, I believe that global variables are the solution to this:

public class Main
{ 
    public static string User_ID { get; set; }
}

a public variable Static(global) can be called in other application formats with the class name and its name:

Main.User_ID

for more details search for global types.

0

You can create a property of your class type in the form you want to receive the object and pass the object in the form builder, see an example below.

Form that will receive the object:

public partial class Form2 : Form
{
    private User user;

    public Form2(User user)
    {
        InitializeComponent();

        this.user = user;
    }
}

And the form that will send the object to the form it is calling:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
        User user = new User
        {
            User_ID = 1,
            Name = "Fulano",
            Email = "[email protected]"
        };

        var form = new Form2(user);            
        form.Show();            
    }
}

This way you can manipulate the values of your object within the form without any kind of interference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.