在将属性从一个类传递到另一个类时出错 [英] Something wrong while passing property from one class to the other

查看:111
本文介绍了在将属性从一个类传递到另一个类时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个班。一个是AuthenticateUser,它允许我设置和获取用户信息,例如用户名和密码。在其他类中,AddEntryWindow是一个WinForm,其中我试图显示来自AuthenticateUser的Password和UserName属性的内容。第三类是另一个WinForm类,它允许我为AuthenticateUser类设置用户名和密码。在我尝试,在这个简化的例子中,要显示从WinForm类的用户名和密码,我得到一个空白的消息框。此外,当在AuthenticateUserWindow中使用另一个消息框时,我可以获得属性的内容。



如何解决此问题,以便能够在AddEntryWindow中查看属性的内容类?



可能是这样的: AuthenticateUser authenticateUser = new AuthenticateUser(); 创建一个新对象。



<$>

p $ p> 使用系统;
using System.Windows.Forms;
//需要与StringBuilder一起使用
使用System.Text;
//需要与ArrayList一起使用。
using System.Collections;

命名空间Store_Passwords_and_Serial_Codes
{
public partial class AddEntryWindow:Form
{
//使身份验证成为可能。
AuthenticateUser authenticateUser = new AuthenticateUser();

//默认构造函数初始化窗体。
public AddEntryWindow()
{
InitializeComponent();
}

private void btnAddEntry_Click(object sender,EventArgs e)
{
MessageBox.Show(authenticateUser.UserName + authenticateUser.Password);
}
}
}

AuthenticateUser.cs

 使用系统; 

命名空间Store_Passwords_and_Serial_Codes
{
类AuthenticateUser
{
私有字符串userName,password;

public AuthenticateUser()
{
}

public AuthenticateUser(string userNamePassed,string passwordPassed)
{
this。用户名= userNamePassed;
this.password = passwordPassed;
}

public string UserName
{
get
{
return userName;
}
set
{
userName = value;
}
}

public string密码
{
get
{
返回密码;
}
set
{
password = value;
}
}
}
}

strong> AuthenticateUserWindow.cs

  
using System.Windows.Forms;

命名空间Store_Passwords_and_Serial_Codes
{
public partial class AuthenticationWindow:Form
{
//需要输入最重要的登录信息
/ /用于加密和解密二进制文件。
AuthenticateUser authenticateUser;

public AuthenticationWindow()
{
InitializeComponent();
}

private void btnClose_Click(object sender,EventArgs e)
{
//关闭认证窗口。
Close();
}

private void btnClear_Click(object sender,EventArgs e)
{
//清除文本框txtUserName和txtPassword
//清除表单按钮单击。
txtUserName.Clear();
txtPassword.Clear();
}

private void btnAuthenticate_Click(object sender,EventArgs e)
{
if(txtUserName.Text == string.Empty || txtPassword.Text == string 。空)
{
MessageBox.Show(请先填写两个信息。
}
else
{
//将值传递给对象AuthenticateUser。
authenticateUser = new AuthenticateUser(txtUserName.Text,txtPassword.Text);

MessageBox.Show(authenticateUser.UserName + authenticateUser.Password);

Close();
}
}
}
}

解决方案

像John说的,你需要修改代码如下:

  public partial class AddEntryWindow:Form 
{
//使身份验证成为可能。
AuthenticateUser authenticateUser = new AuthenticateUser();
//默认构造函数来初始化窗体。
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
{
//最重要的登录信息需要输入
//用于加密和解密二进制文件。
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(请先填写两个信息。
}
else
{
//将值传递给对象AuthenticateUser。
authenticateUser.UserName = txtUserName.Text;
authenticateUser.Password = txtPassword.Text;

MessageBox.Show(authenticateUser.UserName + authenticateUser.Password);

Close();
}
}
}


I am having 3 classes. One is AuthenticateUser which let me to set and get user information such as username and password. In other class, AddEntryWindow is a WinForm in which I am trying to display content of the Password and UserName properties from AuthenticateUser. third class is another WinForm class which let me to set username and password to the AuthenticateUser class. As I try, in this simplified example, to display username and password from a WinForm class I am getting a blank message box. Also, when using another message box in AuthenticateUserWindow I am able to get the content of properties.

How can I fix this to be able to view content of the property in AddEntryWindow class? I have been staring blank for past hour on this.

Probably it's something with line: AuthenticateUser authenticateUser = new AuthenticateUser(); which create a new object. But where would it go instead?

Most likely problem in AddEntryWindow.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();
            }
        }
    }
}

Regards.

解决方案

Like John said, you need to change the code as follows:

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();
        }
    }
}

这篇关于在将属性从一个类传递到另一个类时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆