Search code examples
c#classtextboxgetset

c# getting a string from textbox using get set , than sending it another class?


I have a homework that i should make an accounting application with user login.My problem is , i have to use {get,set} but i don't know how to use them properly.there is one class called 'UserLogin.cs' and one form 'UserLoginForm.cs'.In userLoginForm there is two maskedTextBoxes : maskedTextBoxID and maskedTextBoxPass.

When i click login button on userLoginForm, it calls a function from userloginclass to check if id and pass are correct or not?

The problem is when i click the login button, the id and pass coming from maskedTextBoxes is not that i entered before click login , they are always maskedTextBoxes's first value : empty...

if i enter 123 as id and 123 as pass then if i check them in function to if id is "123" and pass is "123",, there is wrong id&pass message occurs.

this time:if i enter 123 as id and 123 as pass , then if check them in function to if id is " " and pass is " " (which is their first value) the login is sucsesful.

where is the problem?

in userlogin form

    public partial class UserLoginForm : Form
{
    public UserLoginForm()
    {
        InitializeComponent();
    }
    public string userID
    {
        get
        {
            return maskedTextBoxID.Text;
        }
    }

    public string userPASS
    {
        get
        {
            return maskedTextBoxPass.Text;
        }
    }


    private void button_enter_Click(object sender, EventArgs e)
    {
        UserLogin ulogcs = new UserLogin();
        ulogcs.checklogin();

in user login class:

    public void checklogin()
    {
        UserLoginForm uform = new UserLoginForm();
        if (uform.userID == "123" && uform.userPASS == "123")
        {
            MessageBox.Show("Welcome to the bank");
            AccountForm aform = new AccountForm();
            aform.Show();
        }
        else
        {
            MessageBox.Show("Error");
        }
    }

sorry for grammar (i am not native speaker)


Solution

  • You are very close. Because you are instantiating a new UserLoginForm object in your "UserLogin" class, it starts with default data (the empty strings) not the data your user entered.

    The quickest fix is to just pass the form:

    private void button_enter_Click(object sender, EventArgs e)
    {
        UserLogin ulogcs = new UserLogin(this);
        ulogcs.checklogin();
    

    in user login class:

    private UserLoginForm uform;
    
    public UserLogin(UserLoginForm loginForm)
    {
       uform = loginForm;
    }
    
    public void checklogin()
    {       
        if (uform.userID == "123" && uform.userPASS == "123")
        ...
    }
    

    In this code, we use the this keyword to get a reference to the current object (the form itself) and pass it to a parameterized constructor of the UserLogin class. It stores this off to get the data later when we call checklogin.

    Better would to just pass the data:

    private void button_enter_Click(object sender, EventArgs e)
    {
        UserLogin ulogcs = new UserLogin();
        ulogcs.checklogin(userID, userPASS);
    

    in user login class:

    public void checklogin(string user, string password)
    {       
        if (user == "123" && password == "123")
        ...
    }
    

    In this code we recognize that userlogin shouldn't care where the data comes from, it just needs the data. Thus, we change the signature of checklogin to accept the username and password and just use them. No reference to the form (or knowledge that it even exists) is required.