Surface
Surface

Reputation: 11

C# how to get value from another class?

This is my Main.xaml.cs

private void Button_Click(object sender, RoutedEventArgs e)
{

    User u = new User();
    u.id = 1;
    u.access = 2;
    u.username = "USER1";

}

This is User.cs

class User
{
    public int id;
    public int access;
    public string username;

    public int ID
    {
        get { return id; }
        set { id = value; }
    }

    public int Access
    {
        get { return access; }
        set { access = value; }
    }

    public string Username
    {
        get { return username; }
        set { username = value; }
    }

}

Aim:

I have created another button in Main.xaml.cs:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    User u = new User();
    MessageBox.Show(u.Username.ToString());
}

I am aiming to get the value from User.cs that I previously assigned in other button. Button_Click.

How can I show messagebox with values assigned to User.cs?

Upvotes: 1

Views: 958

Answers (3)

Just Shadow
Just Shadow

Reputation: 11921

The reason is that you are creating an object in the scope of Button_Click,
which then is not available to the outer scopes.

To solve the problem you can simply move out the declaration of the User object outside,
and in the meethods, simply assign values to them.

Here is a solution based on your code:

using System;


class User
{
    public int id;
    public int access;
    public string username;

    public int ID
    {
        get { return id; }
        set { id = value; }
    }

    public int Access
    {
        get { return access; }
        set { access = value; }
    }

    public string Username
    {
        get { return username; }
        set { username = value; }
    }
}

public class Program
{
    User u; // Moved the object outside the method.

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        u = new User(); // Assigned value
        u.id = 1;
        u.access = 2;
        u.username = "USER1";
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        // User u = new User(); //  No need of this
        MessageBox.Show(u.Username.ToString());
    }

    public static void Main()
    {
        Console.WriteLine("Hello World");
    }
}

Upvotes: 0

SomeBody
SomeBody

Reputation: 8743

Your variables are local variables only visible in the methods where you declare them. Instead, declare your user variable in your class:

class Main
 {
 private User user;

private void Button_Click(object sender, RoutedEventArgs e)
{

    user = new User();
    user.id = 1;
    user.access = 2;
    user.username = "USER1";

}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    if(user == null)
     {
      MessageBox.Show("create user first");
      }
     else
     {
    MessageBox.Show(user.Username.ToString());
    }
}
}

Now both methods will use the same instance of your User class.

By the way, you can use auto properties in your user class. It is easier to type and less error prone:

class User
 {
 public int ID {get; set; }
 public int Access {get;set;}
 public string Username {get;set;}
 }

Upvotes: 1

Marco Salerno
Marco Salerno

Reputation: 5201

In your code you are creating 2 different objects.

Try this way:

User _user = new User();

private void Button_Click(object sender, RoutedEventArgs e)
{

    _user.id = 1;
    _user.access = 2;
    _user.username = "USER1";

}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    MessageBox.Show(_user.Username);
}

Upvotes: 2

Related Questions