Something wrong while passing property from one cl

2019-09-16 05:26发布

问题:

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.

回答1:

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


回答2:

You are creating two separate instances of AuthenticateUser, one in AddEntryWindow and one in AuthenticationWindow. Just because they have the same name doesn't mean they are in the same scope.

Without knowing how your program works I can't give you the best option, but one would be to create a static class that acts as a global for AuthenticateUser. Another option would be to figure out how to pass the information between your Forms. If you create the AuthenticationWindow from the AddEntryWindow you could set an instance of AuthenticateUser as public in AuthenticationWindow and then access it when the window closes before you dispose of it.



回答3:

Here i could infer a bit of workaround for your question, i do hope it is what your looking for.

If your invoking or launching AddWindowEntry form from AuthenticationWindow assuming that AuthenticationWindow is the parent form, then you have created AuthenticateUser object in this class. Pass this instance to the child form i.e AddWindowEntry and set to a local variable there. So when btnAddEntry_Click event gets executed, then you can display this local variables content in the message box.