简体   繁体   中英

C# How to create a class that stores string values from textboxes

I'm relatively new in C# and in this website.

I'm currently trying to make this application that stores employees' data (eg name, surname, id number, salary) and shows the selected employee's data upon command.
My problem is that I do not know what kind of class to create and how to build it as well as how to call it each time I click the "save button" to create a new instance of that class which will save the data drawn from the Form's textboxes.

I have searched in forums and have managed to write this inside the Form:

private void button1_Click(object sender, EventArgs e) {

    Employee newemployee = new Employee();

    {
        string fname = textBox1.Text;
        string sname = textBox2.Text;
        string id = textBox3.Text;
        string sal = textBox4.Text;

        label5.Text = fname;
        label6.Text = sname;
        label7.Text = id;
        label8.Text = sal;
    }
}

and this inside the class:

public class Employee {
    public string fname { get; set; }
    public string sname { get; set; }
    public string id { get; set; }
    public string sal { get; set; }
}

but as a result ofcourse, the class is not used at all (obviously because its not completed) and labels are printed straight through the textboxes.

Note: I put the labels there in order to test the class during the process.

In your class, you have already created string values, which will be created when you create the instance of your class:

Employee newemployee = new Employee();

This creates memory space for all the variables declared in your class

public class Employee 
{
    public string fname { get; set; }
    public string sname { get; set; }
    public string id { get; set; }
    public string sal { get; set; }
}

What you are doing is then creating additional strings by saying:

string fname = textBox1.Text;
string sname = textBox2.Text;
string id = textBox3.Text;
string sal = textBox4.Text;

So to when you initialize the class it created the variables which you should use for that instance of the class. The following code represents the initializing of the class and using its variables from your example code:

Employee newemployee = new Employee();

newemployee.fname = textBox1.Text;
newemployee.sname = textBox2.Text;
newemployee.id = textBox3.Text;
newemployee.sal = textBox4.Text;

label5.Text = newemployee.fname;
label6.Text = newemployee.sname;
label7.Text = newemployee.id;
label8.Text = newemployee.sal;

Hope that helps and it explains where you went wrong.

You would instantiate your class like this:

Employee newEmployee = new Employee()
    {
        fname = textBox1.Text,
        sname = textBox2.Text,
        id = textBox3.Text,
        sal = textBox4.Text
    };

and then write a method to save your Employee in a database/file which you call after instantiating in the click event.

You're not initialising your class in your calling script, so it can't see it. I suggest having a read of the MS Docs: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/classes

It has an example that does pretty much what you need it to do.

you can create instance and from the class and set the properties

Employee newEmp = new Employee();
newEmp.fname = textBox1.Text;
newEmp.sname = textBox2.Text;
newEmp.id = textBox3.Text;
newEmp.sal = textBox4.Text;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM