在将属性从一个类传递到另一个类时出错了
本文关键字:错了 出错 另一个 一个 属性 | 更新日期: 2023-09-27 18:13:23
我有3类。一个是AuthenticateUser,它允许我设置和获取用户信息,如用户名和密码。在其他类中,AddEntryWindow是一个WinForm,我试图从AuthenticateUser显示密码和用户名属性的内容。第三个类是另一个WinForm类,它允许我为AuthenticateUser类设置用户名和密码。在这个简化的示例中,当我尝试从WinForm类显示用户名和密码时,我得到了一个空白消息框。此外,当使用AuthenticateUserWindow中的另一个消息框时,我能够获得属性的内容。
我怎么能解决这个问题,以便能够查看AddEntryWindow类属性的内容?
我一小时前一直在想这件事。可能是一行:AuthenticateUser authenticateUser = new AuthenticateUser();
创建了一个新对象。但它会去哪里呢?
addentrywindows .cs最可能出现的问题
using System;
using System.Windows.Forms;
// Needed to be used with StringBuilder
using System.Text;
// Needed to be used with ArrayList.
using System.Collections;
namespace Store_Passwords_and_Serial_Codes
{
public partial class AddEntryWindow : Form
{
// Making authentication possible.
AuthenticateUser authenticateUser = new AuthenticateUser();
// Default constructor to initialize the form.
public AddEntryWindow()
{
InitializeComponent();
}
private void btnAddEntry_Click(object sender, EventArgs e)
{
MessageBox.Show(authenticateUser.UserName + authenticateUser.Password);
}
}
}
AuthenticateUser.cs
using System;
namespace Store_Passwords_and_Serial_Codes
{
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;
}
}
}
}
AuthenticateUserWindow.cs
using System;
using System.Windows.Forms;
namespace Store_Passwords_and_Serial_Codes
{
public partial class AuthenticationWindow : Form
{
// Most important log in information needs to be entered
// for encrypting and decrypting binary file.
AuthenticateUser authenticateUser;
public AuthenticationWindow()
{
InitializeComponent();
}
private void btnClose_Click(object sender, EventArgs e)
{
// Closing Authentication Window form.
Close();
}
private void btnClear_Click(object sender, EventArgs e)
{
// Clearing text boxes txtUserName and txtPassword
// after Clear Form button is clicked.
txtUserName.Clear();
txtPassword.Clear();
}
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
{
// Passing the values to object AuthenticateUser.
authenticateUser = new AuthenticateUser(txtUserName.Text, txtPassword.Text);
MessageBox.Show(authenticateUser.UserName + authenticateUser.Password);
Close();
}
}
}
}
问候。
就像John说的,您需要按照如下方式更改代码:
public partial class AddEntryWindow : Form
{
// Making authentication possible.
AuthenticateUser authenticateUser = new AuthenticateUser();
// Default constructor to initialize the form.
public AddEntryWindow()
{
InitializeComponent();
}
private void btnAddEntry_Click(object sender, EventArgs e)
{
new AuthenticationWindow(authenticateUser).ShowDialog();
MessageBox.Show(authenticateUser.UserName + authenticateUser.Password);
}
}
...
public partial class AuthenticationWindow : Form
{
// Most important log in information needs to be entered
// for encrypting and decrypting binary file.
AuthenticateUser authenticateUser;
public AuthenticationWindow(AuthenticateUser user)
{
InitializeComponent();
authenticateUser = user;
}
...
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
{
// Passing the values to object AuthenticateUser.
authenticateUser.UserName = txtUserName.Text;
authenticateUser.Password = txtPassword.Text;
MessageBox.Show(authenticateUser.UserName + authenticateUser.Password);
Close();
}
}
}
您正在创建两个独立的AuthenticateUser
实例,一个在AddEntryWindow
中,一个在AuthenticationWindow
中。仅仅因为它们有相同的名称并不意味着它们在同一个scope
中。
不知道你的程序是如何工作的,我不能给你最好的选择,但一个是创建一个static class
作为AuthenticateUser
的全局。另一种选择是找出如何在Forms
之间传递信息。如果您从AddEntryWindow
创建AuthenticationWindow
,您可以在AuthenticationWindow
中将AuthenticateUser
的实例设置为公共,然后在窗口关闭时访问它,然后再对其进行dispose
。
在这里,我可以为你的问题推断出一些变通方法,我希望这是你所寻找的。
如果你从AuthenticationWindow调用或启动AddWindowEntry表单,假设AuthenticationWindow是父窗体,那么你已经在这个类中创建了AuthenticateUser对象。将此实例传递给子窗体,即AddWindowEntry,并在那里设置一个本地变量。因此,当btnAddEntry_Click事件被执行时,您可以在消息框中显示这个局部变量的内容。