在不同的类上使用相同的对象,同时保留一个对象的内容
本文关键字:保留 对象 一个对象 | 更新日期: 2023-09-27 18:13:25
我有3类。第一个类是一个Authorize类,它有User和Pass的get/set属性。其次,添加类创建了一个新的授权类实例,并通过初始化新对象将文本框中的值赋给属性:
Authorize authorize = new Authorize();
第三个类Display试图从类Authorize中获取属性User和Pass的值。问题是我不能在这里使用authorization的新对象,因为它将清空第一个创建对象的内容。
// Can not do, because the object will be discarded and new one created.
Authorize authorize = new Authorize();
我怎样才能避免/改变这一点,这样我就可以从不同的类访问相同的对象?这是一个理论上的例子。我正在写代码,但它很长。如果需要,我可以贴在这里。但现在我离开了。
如果我说得不够清楚,请提问。
强烈推荐:)
对
——编辑——一些代码:
AddEntryWindow。
namespace Store_Passwords_and_Serial_Codes
{
public partial class AddEntryWindow : Form
{
private string user, pass;
// Initializind ArrayList to store all data needed to be added or retrived.
static private ArrayList addedEntry = new ArrayList();
// Initializing MainWindow form.
MainWindow mainWindow;
// Making authentication possible.
// AuthenticateUser authenticateUser = new AuthenticateUser();
EncryptDecrypt en = new EncryptDecrypt();
// Default constructor to initialize the form.
public AddEntryWindow()
{
InitializeComponent();
}
public AddEntryWindow(MainWindow viaParameter) : this()
{
mainWindow = viaParameter;
}
public AddEntryWindow(string user, string pass)
{
this.user = user;
this.pass = pass;
}
private void btnAddEntry_Click(object sender, EventArgs e)
{
// Making sure that type is selected.
if {}
else
{
// reason why I need the content of AuthenticationUser content.
string encrypted = en.Encrypt(stringBuilder.ToString(), user, pass);
string decrypted = en.Decrypt(encrypted, user, pass);
MessageBox.Show(user + pass);
//encrypted + Environment.NewLine + decrypted;
/*mainWindow.ChangeTextBox = stringBuilder.ToString() + Environment.NewLine +
"Encrypted" + Environment.NewLine +
encrypted + Environment.NewLine +
"Decrypted" + Environment.NewLine +
decrypted + Environment.NewLine;
*/
}
}
public static void ShowMe(AuthenticateUser au)
{
au.UserName = user;
au.Password = pass;
}
private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
{
// Deciding which data must be entered depending on
// what type is selected from combo box.
// PC
// Web Site
// Serial Code
}
}
}
authenticationwindows .cs as Add class
namespace Store_Passwords_and_Serial_Codes
{
public partial class AuthenticationWindow : Form
{
public AuthenticationWindow()
{
InitializeComponent();
}
private void btnAuthenticate_Click(object sender, EventArgs e)
{
if (txtUserName.Text == string.Empty || txtPassword.Text == string.Empty)
{
MessageBox.Show("Please fill both information first.");
}
else
{
AuthenticateUser au = new AuthenticateUser();
au.UserName = txtUserName.Text;
au.Password = txtPassword.Text;
AddEntryWindow.ShowMe(au);
MessageBox.Show(au.UserName + au.Password);
Close();
}
}
}
}
不重要的AuthenticateUser.cs作为授权类
using System;
namespace Store_Passwords_and_Serial_Codes
{
public class AuthenticateUser
{
private string userName, password;
public AuthenticateUser()
{
}
public AuthenticateUser(string userNamePassed, string passwordPassed)
{
this.userName = userNamePassed;
this.password = passwordPassed;
}
public string UserName
{
get
{
return userName;
}
set
{
userName = value;
}
}
public string Password
{
get
{
return password;
}
set
{
password = value;
}
}
}
}
Edit:
你的问题讲得更清楚一些,所以我试着解决它。
在主表单中,让你的代码像这样做:
private AuthenticateUser storedAuth; // shared data...
private void btnLogin_Click(object sender, EventArgs e)
{
AuthorizeWindow authWindow = new AuthorizeWindow();
authWindow.ShowDialog();
storedAuth = authWindow.Result; // Get the auth result back...
}
在AuthorizeWindow
中,创建一个result属性,并在单击OK并填写完所有数据时设置它:
public AuthenticateUser Result { get; set; }
private void btnAuthenticate_Click(object sender, EventArgs e)
{
if (txtUserName.Text == string.Empty || txtPassword.Text == string.Empty)
{
MessageBox.Show("Please fill both information first.");
}
else
{
// Don't try to call the other window here, just set the result and close...
Result = new AuthenticateUser();
Result.UserName = txtUserName.Text;
Result.Password = txtPassword.Text;
Close();
}
}
然后在主表单上,当您创建并打开AddEntryWindow
时,将您存储的身份验证传递给它:
private void btnAdd_Click(object sender, EventArgs e)
{
AddEntryWindow addWindow =
new AddEntryWindow(storedAuth.User, storedAuth.Password);
addWindow.ShowDialog();
}
但是,哎呀,我在这里创建了一个小错误。我们应该先检查它们是否通过了身份验证:
private void btnAdd_Click(object sender, EventArgs e)
{
if(storedAuth == null)
{
MessageBox.Show("You must log in before you add an entry");
}
else
{
AddEntryWindow addWindow =
new AddEntryWindow(storedAuth.User, storedAuth.Password);
addWindow.ShowDialog();
}
}
同时,我在你的AddEntryWindow
代码中发现了一个小bug。确保所有窗口的所有构造函数总是调用InitializeComponent
。在这种情况下,编辑接受用户名和密码的构造函数来调用默认构造函数,就像其他接受MainWindow
的构造函数一样:
public AddEntryWindow(string user, string pass)
: this() // important!
{
this.user = user;
this.pass = pass;
}
编辑前:
我不完全理解你的示例代码。如果您创建新的代码示例来添加到您的问题中,则会更容易。
我从你的话中得到的是:
public class Authorize
{
public string Username { get; set; }
public string Password { get; set; }
}
public class Add
{
public void Login()
{
Authorize authorize = new Authorize();
authorize.Username = usernameTextBox.Text;
authorize.Password = passwordTextBox.Text;
// Todo: Rest of login logic here
}
// Todo: Other code here...
}
public class Display
{
public void Show()
{
Authorize authorize = new Authorize();
// uh oh, Username and Password are null!
}
// Todo: Other code here...
}
<<p> 解决方案/strong> 最简单的方法是将Authorize
的实例传递给Display
类中的Show
方法(或您实际调用的任何方法)。只需接受Authorize
的实例作为该方法的参数:
public class Display
{
public void Show(Authorize authorize)
{
// Now we have the values that the Login method created...
}
// ...
}
public class Add
{
public void Login()
{
Authorize authorize = new Authorize();
authorize.Username = usernameTextBox.Text;
authorize.Password = passwordTextBox.Text;
display.Show(authorize);
}
// ...
}
有两种方法可以实现您正在寻找的目标。第一种方法是将Authority的单个实例传递给将使用它的所有不同方法。我猜出于某种原因这是禁止的,否则你已经在这么做了。有一种称为单例的设计模式可能对您正在做的事情很有用。链接页面有一些代码示例
作为参数传递是最好的解决方案,如果你在作为参数传递时有问题,那么去授权作为属性(Set/Get),这样你就可以摆脱参数传递的问题。如果这也是一个问题,那么选择单例模式或创建静态类。