Employee和ProductionWorker类,从类中获取信息

本文关键字:获取 信息 ProductionWorker Employee | 更新日期: 2023-09-27 18:26:04

我不完全确定标题是否适合。我被学校的一项作业卡住了,本该有一段视频来展示如何做到这一点,但就我而言,我不知道如何从pearson网站下载学生文件。以下是我正在处理的问题。

员工和生产工人类

创建一个Employee类,该类具有以下数据的属性:

  • 员工姓名
  • 员工编号

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

  • 移位数(整数,如1、2或3)
  • 小时工资率

工作日分为两班:白天和晚上。Shift属性将包含一个整数值,表示员工工作的班次。白班是1班,夜班是2班。

创建一个应用程序,该应用程序创建ProductionWorker类的对象,并允许用户为该对象的每个属性输入数据。检索对象的属性并显示其值。

这是我为它工作的代码:

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的东西在那里,因为我不知道如何使用移位代码,或多或少地,我把它用作占位符,直到我把它指出来。我不知道如何解决这个问题,希望这是个小错误。

多亏了通讯器的帮助,我才解决了第一个问题,现在我的消息框在最后出现了问题。我输入的信息是,Glitter代表姓名,12代表身份证号码,1代表轮班,12代表工资。显示内容如下:

名称System.Windows.Forms.TextBox,文本:GlitterIDNumber 0小时费率System.Windows.Forms.TextBox,Text:Shift-SystemWindowsForm.TextBox,Text:

Employee和ProductionWorker类,从类中获取信息

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()

CCD_ 7如下

   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);
     }

}

我添加了评论来详细说明我所做的所有更改,如果你有任何问题,请给我写一条评论。

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