因此情况是用户已使用凭据登录,并且当他们将商品添加到购物车时,我希望将全名从Account.cs
转移到ProductGUI.cs
< / p>
这是我尝试过的方法,但它会提示出一个空的控制台声明。
很抱歉,如果我问一个重复的问题,但我需要帮助来弄清楚如何专门解决这个问题。
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
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
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!");
}
}
答案 0 :(得分:0)
您必须使您的Account
实例为Login-GUI和Product-GUI都知道。最简单(也是最丑陋,最不推荐)的方法是将其设为静态:
class LoginGUI
{
public static Account acc;
[...]
}
class ProductGUI
{
private void addToCartButton_Click(object sender, EventArgs e)
{
Console.WriteLine(LoginGUI.acc.Fullname);
}
[...]
}
但实际上,对单例模式有一个很好的阅读。 依赖注入也可能有所帮助。
答案 1 :(得分:0)
您可以通过创建fullname的静态属性来实现此目的。它们对于抽象程序中的全局数据非常有用。