Accessing a variable from another class c# -
so scenario user has logged in credentials, , when have added item cart, want full name taken account.cs
productgui.cs
this method i've tried, prompts out empty console statement.
sorry if i'm asking duplicate question, need figure out how solve problem.
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 { { return fullname; } set { fullname = value; } }
productgui.cs
private void addtocartbutton_click(object sender, eventargs e) { // believe i'm creating new account based question, how pass information without creating new account. account = 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 information account. acc = new account(fullname, username, email, password); temp = true; } } if (temp) { messagebox.show("welcome anime fanatic.\n enjoy stay!"); this.hide(); mainpagegui mainpage = new mainpagegui(); mainpage.showdialog(); } else { messagebox.show("you have entered incorrect username or password.\n please try again!"); } }
you have make account
instance known both login-gui , product-gui. easiest (and ugly , not recommended) way this, make 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 read singleton pattern. , dependency injection might help, too.
Comments
Post a Comment