Pass values from one object to another form C # [duplicate]

2

I have a system, where after the user logs in, I pull a XML from various data about it and I save on attributes of an object, however there is another form to do the password change, where I need the User_ID of the first How can I pass values from an object to another form in C #? Thanks

    
asked by anonymous 25.11.2017 / 17:13

2 answers

2

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

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

A public static (global) variable can be called in other forms of the application with the name of the class and its name:

Main.User_ID

For more details, search for global types.

    
25.11.2017 / 17:32
0

You can create a property of the type of your class in the form you want to receive the object and pass the object in the form pain 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();            
    }
}

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

    
25.11.2017 / 18:03