简体   繁体   English

从另一个类C#访问变量

[英]Accessing a variable from another class c#

So the scenario is that a user has already logged in with the credentials, and when they have added an item to a cart, i want the full name to be taken from Account.cs to ProductGUI.cs 因此,场景是用户已经使用凭据登录,并且当他们将商品添加到购物车时,我希望将全名从Account.cs提取到ProductGUI.cs

This is the method that I've tried, but it prompts out an empty Console statement. 这是我尝试过的方法,但是会提示出一个空的Console语句。

Sorry if I'm asking a duplicate question, but i need help to figure out how to specifically solve this problem. 抱歉,如果我问一个重复的问题,但我需要帮助以弄清楚如何专门解决此问题。

Account.cs Account.cs

    private string fullname;
    private string username;
    private string email;
    private string password;

    public Account(string fullname, string username, string email, string password)
    {
        this.fullname = fullname;
        this.username = username;
        this.email = email;
        this.password = password;
    }

    public string Fullname
    {
        get
        {
            return fullname;
        }
        set
        {
            fullname = value;
        }
    }

ProductGUI.cs ProductGUI.cs

private void addToCartButton_Click(object sender, EventArgs e)
    {
        // I believe I'm creating a new account based another question, but how do i pass the information without creating a new account.
        Account a = new Account();
        Console.WriteLine(a.Fullname);
    }

LoginGUI.cs LoginGUI.cs

private void signinButton_Click(object sender, EventArgs e)
    {
        bool temp = false;
        foreach (DataRow row in dt.Rows)
        {
            if (row["Username"].ToString() == usernameTextbox.Text && row["Password"].ToString() == passwordTextbox.Text)
            {
                string fullname = row["Fullname"].ToString();
                string username = row["Username"].ToString();
                string email = row["Email"].ToString();
                string password = row["Password"].ToString();

                // I've saved the information into account.
                acc = new Account(fullname, username, email, password);
                temp = true;
            }
        }
        if (temp)
        {
            MessageBox.Show("Welcome to Anime Fanatic.\n Enjoy your stay!");
            this.Hide();
            mainPageGUI mainPage = new mainPageGUI();
            mainPage.ShowDialog();
        }
        else
        {
            MessageBox.Show("You have entered an incorrect Username or Password.\n Please try again!");
        }
    }

You have to make your Account instance known to both your Login-GUI and your Product-GUI. 您必须使Login-GUI和Product-GUI都知道您的Account实例。 The easiest (and most ugly and most not recommended) way to do this, is to make it static: 最简单(最丑陋,最不推荐)的方法是使其静态:

class LoginGUI
{
    public static Account acc;

    [...]
}

class ProductGUI
{
    private void addToCartButton_Click(object sender, EventArgs e)
    {
        Console.WriteLine(LoginGUI.acc.Fullname);
    }

    [...]
}

But really, have a good read about singleton pattern . 但是,确实,对单例模式有很好的了解。 And dependency injection might help, too. 依赖注入也可能会有所帮助。

You can achieve this by creating static property of fullname. 您可以通过创建全名的静态属性来实现。 They are useful for abstracting global data in programs. 它们对于在程序中抽象全局数据很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM