将两位数据放入一个c#窗体中

时间:2017-05-30 11:35:02

标签: c# winforms

我从文本文件中获取数据以填充带有数据的文本框。但是我试图将员工的工资从另一个班级拿到文本框中,我正在努力这样做。我的第一堂课是带有此代码的员工类:

public class Employee
{
    string _firstName;
    string _lastName;
    string _address;
    string _postCode;
    string _phoneNumber;
    DateTime _dateOfBirth;

    public Employee()
    {
    }

    public Employee(string firstName, string lastName, string address, string postCode, string phoneNumber, DateTime dateOfBirth)
    {
        _firstName = firstName;
        _lastName = lastName;
        _address = address;
        _postCode = postCode;
        _phoneNumber = phoneNumber;
        _dateOfBirth = dateOfBirth;
    }

    public string firstName
    {
        get
        {
            return _firstName;
        }
        set
        {
            _firstName = value;
        }
    }

    public string lastName
    {
        get
        {
            return _lastName;
        }
        set
        {
            _lastName = value;
        }
    }

    public string address
    {
        get
        {
            return _address;
        }
        set
        {
            _address = value;
        }
    }

    public string postCode
    {
        get
        {
            return _postCode;
        }
        set
        {
            _postCode = value;
        }
    }

    public string phoneNumber
    {
        get
        {
            return _phoneNumber;
        }
        set
        {
            _phoneNumber = value;
        }
    }

    public DateTime dateOfBirth
    {
        get
        {
            return _dateOfBirth;
        }
        set
        {
            _dateOfBirth = value;
        }
    }

后面是带有此代码的带薪班级:

 public class SalariedEmployee : Employee
 {
    decimal _salary;

    public SalariedEmployee(string firstName, string lastName, string 
   address, string postCode, string phoneNumber, DateTime dateOfBirth, 
 decimal salary) : base(firstName, lastName, address, postCode, phoneNumber, 
 dateOfBirth)
    {
        _salary = salary;
    }

    public decimal salary
    {
        get
        {
            return _salary;
        }
        set
        {
            _salary = value;
        }
    }
 }

然后进入加载方法,如下所示:

public bool Load(string employeesFile)    
{
    List<string> lines = new List<string>();

    using (StreamReader reader = new StreamReader("employees.txt"))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            //Splitting the data using |
            string[] temp = line.Split('|');

            int year = Convert.ToInt32(temp[5]);
            int month = Convert.ToInt32(temp[6]);
            int day = Convert.ToInt32(temp[7]);

            //This is to populate an employees detials 
            Employee emp = new Employee
            {
                firstName = temp[0],
                lastName = temp[1],
                address = temp[2],
                postCode = temp[3],
                phoneNumber = temp[4],
                dateOfBirth = new DateTime(year, month, day)
            };

            //This class is from List, so I used the add method to add the employee.
            Add(emp);
        }
        return true;

最后是表单代码:

public Salaried_Employee_Details(Employee emp)
{
    InitializeComponent();


    textBoxLastName.Text = emp.lastName;
    textBoxFirstName.Text = emp.firstName;
    textBoxAddress.Text = emp.address;
    textBoxPostCode.Text = emp.postCode;
    textBoxPhoneNumber.Text = emp.phoneNumber;
    dateTimeDateOfBirth.Text = emp.dateOfBirth.ToString();
    //textBoxSalary.Text = emp.salary;
}

这里的工作表格: Salary form

文本文件的格式如下:

Smyth|Jack|London street, London City, London|01142325413|1990|3|21|37000

那么如何将受薪数据放入文本框?

3 个答案:

答案 0 :(得分:2)

查看将字符串拆分为数组,您要么没有邮政编码,要么没有编号:

postCode = temp[3],
phoneNumber = temp[4],

以下是数组的索引

Smyth|Jack|London street, London City, London|01142325413|1990|3|21|37000
  ^     ^                     ^                     ^       ^  ^  ^  ^
  |     |                     |                     |       |  |  |  |
  0     1                     2                     3       4  5  6  7

在这种情况下,4对我来说就像一年!

如果薪水位于最后一个位置,您需要区分正常EmployeeSalariedEmployee在第一种情况下,您就像已经这样做,在第二种情况下,您需要创建一个加载数据时SalariedEmployee对象:

int year = Convert.ToInt32(temp[4]);
int month = Convert.ToInt32(temp[5]);
int day = Convert.ToInt32(temp[6]);

SalariedEmployee emp = new SalariedEmployee
    {
        firstName = temp[1],
        lastName = temp[0],
        address = temp[2],
        phoneNumber = temp[3],
        dateOfBirth = new DateTime(year, month, day)
        salary = Convert.ToDecimal(temp[7]);
    };

编辑:使代码工作所需的是SalariedEmployee类中的无参数构造函数。

public SalariedEmployee()
{

}

或者你需要使用你用所有参数编写的构造函数:

public SalariedEmployee(string firstName, string lastName, string
   address, string postCode, string phoneNumber, DateTime dateOfBirth,
 decimal salary)

看起来像这样:

SalariedEmployee emp = new SalariedEmployee(temp[1], temp[0],temp[2],
                               "I don't know where your postcode is", 
                               temp[3],new DateTime(year, month, day),
                               Convert.ToDecimal(temp[7]));

答案 1 :(得分:0)

没有.salary属性,因为构造函数期望Employee

public Salaried_Employee_Details(Employee emp)

哪个没有这个属性。但是,SalariedEmployee 具有该属性。如果构造函数需要SalariedEmployee,那么只需将其更改为需要一个:

public Salaried_Employee_Details(SalariedEmployee emp)

然后你可以像其他任何一样使用该属性:

textBoxSalary.Text = emp.salary;

答案 2 :(得分:0)

看起来文本文件格式中缺少一个点。

您发布的字符串

  

Smyth |杰克|伦敦街伦敦街,   伦敦| 01142325413 | 1990 | 3 | 21 | 37000

有7个分隔符,它们导致8个字符串的数组,其中最后一个(temp[7])是工资。相反,在您的代码中,它被分配给Employee实例的day属性。

顺便说一句,假设实际上有另一个字符串,其邮政编码格式如下

  

Smyth |杰克|伦敦街,伦敦城,伦敦| L12   3AS | 01142325413 | 1990 | 3 | 21 | 37000

你应该将emp声明为SalariedEmployee类实例,我建议使用TryParse方法检查工资字符串是否可以转换为十进制。

public bool Load(string employeesFile)
    {
        bool isLoaded = false;
        List<string> lines = new List<string>();

        using (StreamReader reader = new StreamReader("employees.txt"))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                //Splitting the data using |
                string[] temp = line.Split('|');

                int year = Convert.ToInt32(temp[5]);
                int month = Convert.ToInt32(temp[6]);
                int day = Convert.ToInt32(temp[7]);

                //Populating the employees details 

                decimal _salary;
                if (Decimal.TryParse(temp[8], out _salary))
                {
                    SalariedEmployee emp = new SalariedEmployee(firstName: temp[1],
                        lastName: temp[0],
                        address: temp[2],
                        postCode: temp[3],
                        phoneNumber: temp[4],
                        dateOfBirth: new DateTime(year, month, day),
                        salary: _salary)

此时,您还需要确保List参数更改为SalariedEmployee类型,否则您将在以下行中收到编译器错误

                Add(emp);
                isLoaded = true;
                }
            }
            return isLoaded;
        }

此时,在表单中你只需要更改类型参数并取消检查最后一行代码

public Salaried_Employee_Details(SalariedEmployee emp)
{
    InitializeComponent();

    textBoxLastName.Text = emp.lastName;
    textBoxFirstName.Text = emp.firstName;
    textBoxAddress.Text = emp.address;
    textBoxPostCode.Text = emp.postCode;
    textBoxPhoneNumber.Text = emp.phoneNumber;
    dateTimeDateOfBirth.Text = emp.dateOfBirth.ToString();
    textBoxSalary.Text = emp.salary;
}

希望这对你有所帮助。

再见! 的Davide。