Employee 和 ProductionWorker 类,正在从 类 获取信息

Employee and ProductionWorker Classes, getting info from classes

我不完全确定标题是否适合这个。我被困在学校的作业上,应该有一个视频来展示如何做到这一点,但对于我来说,我无法弄清楚如何从 pearson 网站下载学生文件。以下是我正在处理的问题。

员工和生产工人类

创建具有以下数据属性的员工 class:

接下来,创建一个名为 ProductionWorker 的 class,它派生自 Employee class。 ProudctionWorker class 应具有用于保存以下数据的属性:

工作日分为白天和晚上两班。班次 属性 将保存一个整数值,表示员工工作的班次。白班是一班,夜班是二班

创建一个应用程序来创建 ProductionWorker class 的 object 并让用户为每个 object 的属性输入数据。检索 object 的属性并显示它们的值。

这是我为它编写的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace classes
{
    public partial class frmMainClasses : Form
    {
        string EmpShift = "";
        string EmployeeName = "";
        int EmployeeNumber = 0;
        float HourlyPayRate = 0;



        public class Employee
        {
            public string EmployeeName { get; set; }
            public int EmployeeNumber { get; set; }
        }

        public class ProductionWorker : Employee
        {
            public float HourlyPayRate { get; set; }
            public Shift Shift { get; set; }
        }

        public enum Shift
        {
            Day = 1,
            Night = 2
        }

        public frmMainClasses()
        {
            InitializeComponent();
        }

        private void btxExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnGetInfo_Click(object sender, EventArgs e)
        {
            string EmpShift = "";
            ProductionWorker productionWorker = new ProductionWorker();
            productionWorker.EmployeeName = txtName.ToString();
            EmployeeName = productionWorker.EmployeeName; //Added mostly because I couldn't get EmployeeName to show anything in the messagebox
            productionWorker.EmployeeNumber = Convert.ToInt32(txtIdNumb.text);
            productionWorker.HourlyPayRate = Convert.ToInt32(txtPay.text);
            EmpShift = Convert.ToString(txtShift.text);
            txtName.Text = "";
            txtIdNumb.Text = "";
            txtPay.Text = "";
            txtShift.Text = "";
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Name " + EmployeeName + "IDNumber " + EmployeeNumber + "Hourly rate " + txtPay + "Shift " + txtShift);

        }
    }

}

代码本身没有显示任何错误,但是当我尝试 运行 时,我得到:

字符串 EmpShift 的东西在那里,因为我无法弄清楚如何使用班次代码或多或少地使用它作为占位符,直到我弄清楚它。我不知道如何解决这个问题,希望它是一个小错误。

感谢 commets 的帮助,我能够解决我遇到的第一个问题,现在我遇到了最后的消息框问题。我输入的信息是,姓名为Glitter,身份证号为12,班次为1,工资为12。这是它显示的内容:

姓名System.Windows.Forms.TextBox,文字:GlitterIDNumber 0小时费率 System.Windows.Forms.TextBox,文本:Shift SystemWindowsForm.TextBox,文本:

Convert.ToString 不会给你一个错误,因为其中之一调用它的重载对象 - public static string ToString(object value)。但是,由于您对用户输入的值感兴趣,请使用 - TextBox.Text 属性 而不是传递 TextBox.

更新 1:关于此行为的一些内部信息

System.Convert.ToString(object value)在.net中实现如下-

public static string ToString(Object value, IFormatProvider provider) {
        IConvertible ic = value as IConvertible;
        if (ic != null) 
            return ic.ToString(provider);
        IFormattable formattable = value as IFormattable;
        if (formattable != null) 
            return formattable.ToString(null, provider);
        return value == null? String.Empty: value.ToString();
    }

因此它最终调用 TextBox.ToString()

System.Convert.ToInt32(object value)如下

   public static int ToInt32(object value) {
        return value == null? 0: ((IConvertible)value).ToInt32(null);
    }

因此无效的转换异常 - ((IConvertible)value).ToInt32(null)

更新 2:重构代码使其工作

public partial class frmMainClasses : Form
{
    //Change 1 
    //I have removed all class level string since they tend to make your code complicated & difficult to manage
    //I'll replace all of them this a single instance of ProductionWorker class, a single object is easy to manage than a bunch 

    ProductionWorker productionWorker = new ProductionWorker(); // Creating the production Worker at class level


    public class Employee
    {
        public string EmployeeName { get; set; }
        public int EmployeeNumber { get; set; }
    }

    public class ProductionWorker : Employee
    {
        public float HourlyPayRate { get; set; }
        public Shift Shift { get; set; }
    }

    public enum Shift
    {
        Day = 1,
        Night = 2
    }

    public frmMainClasses()
    {
        InitializeComponent();
    }

    private void btxExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnGetInfo_Click(object sender, EventArgs e)
    {
        //Change 2 : set the values of the class level variable
        productionWorker.EmployeeName = txtName.Text; //.ToString();  // Change 3 removing the .ToString();
        productionWorker.EmployeeNumber = Convert.ToInt32(txtIdNumb.Text);
        productionWorker.HourlyPayRate = Convert.ToInt32(txtPay.Text);
        productionWorker.Shift = (Shift)Enum.Parse(typeof(Shift), txtShift.Text);

        //change 4 : using .ResetText() instead of Text
        txtName.ResetText();// .Text = "";
        txtIdNumb.ResetText();//.Text = "";
        txtPay.ResetText();//.Text = "";
        txtShift.ResetText();//.Text = "";
    }

    private void btnShow_Click(object sender, EventArgs e)
    {
        // change 5 : accessing class level productionWorker instead of bunch of strings
        MessageBox.Show("Name " + productionWorker.EmployeeName + " IDNumber " + productionWorker.EmployeeNumber + " Hourly rate " + productionWorker.HourlyPayRate + " Shift " + productionWorker.Shift);

     }


}

我添加了评论以详细说明我所做的所有更改,如果您有任何问题,请给我留言。

此外,目前您的代码不验证文本框中的用户输入。