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

user3524633

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)

BradleyDotNET

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Getting TextBox values from another class

From Dev

Not able to getting data from one class to another using *.get*()

From Dev

Getting String info from another java class rather than the same one

From Dev

Getting string from another class and transfering it to url

From Dev

Unable to set a TextBox text owned by MainWindow from another class

From Dev

Getting value from GUI and using it in another class

From Dev

Getting Array from Another class C++

From Dev

Sending information from IRC Class to WinForm TextBox

From Dev

Sending double value from one class to another in C#

From Dev

Open ears text to speech (voice) not working when getting string from another class/controller (IOS, Objective c)

From Dev

Open ears text to speech (voice) not working when getting string from another class/controller (IOS, Objective c)

From Dev

async SMS sending function is not getting data from textbox WPF c#

From Dev

Java Getting String from another class returning null

From Dev

Getting input from text field in ViewController and using in another class

From Dev

Getting input from text field in ViewController and using in another class

From Dev

Getting a variable from a Click event method and using it in another class

From Dev

How to get string from textbox

From Dev

How to get total no seconds from a string in time format greater than 24hrs using C#

From Dev

POCO WebSocket - sending data from another class

From Dev

How to get value of textbox on button click on one form and into a string in another class?

From Dev

How to Bind Value from 1 textbox to another textbox in C# using AngularJS

From Dev

Updating a textbox on a WinForm from another class

From Dev

Sending NSNumber to another class method - Objective C

From Dev

Set a class for a textbox on focus using Javascript

From Dev

Set a class for a textbox on focus using Javascript

From Dev

Getting value from a database using textbox

From Dev

getting the textbox input from javascript using jquery

From Dev

How to set text for JTextField from another class using DocumentFIlter?

Related Related

  1. 1

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

  2. 2

    Getting TextBox values from another class

  3. 3

    Not able to getting data from one class to another using *.get*()

  4. 4

    Getting String info from another java class rather than the same one

  5. 5

    Getting string from another class and transfering it to url

  6. 6

    Unable to set a TextBox text owned by MainWindow from another class

  7. 7

    Getting value from GUI and using it in another class

  8. 8

    Getting Array from Another class C++

  9. 9

    Sending information from IRC Class to WinForm TextBox

  10. 10

    Sending double value from one class to another in C#

  11. 11

    Open ears text to speech (voice) not working when getting string from another class/controller (IOS, Objective c)

  12. 12

    Open ears text to speech (voice) not working when getting string from another class/controller (IOS, Objective c)

  13. 13

    async SMS sending function is not getting data from textbox WPF c#

  14. 14

    Java Getting String from another class returning null

  15. 15

    Getting input from text field in ViewController and using in another class

  16. 16

    Getting input from text field in ViewController and using in another class

  17. 17

    Getting a variable from a Click event method and using it in another class

  18. 18

    How to get string from textbox

  19. 19

    How to get total no seconds from a string in time format greater than 24hrs using C#

  20. 20

    POCO WebSocket - sending data from another class

  21. 21

    How to get value of textbox on button click on one form and into a string in another class?

  22. 22

    How to Bind Value from 1 textbox to another textbox in C# using AngularJS

  23. 23

    Updating a textbox on a WinForm from another class

  24. 24

    Sending NSNumber to another class method - Objective C

  25. 25

    Set a class for a textbox on focus using Javascript

  26. 26

    Set a class for a textbox on focus using Javascript

  27. 27

    Getting value from a database using textbox

  28. 28

    getting the textbox input from javascript using jquery

  29. 29

    How to set text for JTextField from another class using DocumentFIlter?

HotTag

Archive