Employee和ProductionWorker类,从类

时间:2015-12-07 03:45:07

标签: c# class messagebox iconvertible

我不完全确定这个标题是好的。我被困在学校的任务上,假设是一个视频来展示如何做到这一点,但对于我的生活,我无法弄清楚如何从pearson网站下载学生档案。以下是我正在处理的问题。

员工和ProductionWorker类

创建一个具有以下数据属性的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,ID为12,轮班为1,付费为12。以下是它的展示:

名称System.Windows.Forms.TextBox,Text:GlitterIDNumber 0Hourly rate System.Windows.Forms.TextBox,Text:Shift SystemWindowsForm.TextBox,Text:

1 个答案:

答案 0 :(得分:0)

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

     }


}

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

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