1

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

员工和生产工人阶级

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

  • 员工姓名
  • 员工编号

接下来,创建一个派生自 Employee 类的名为 ProductionWorker 的类。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 为 ID 号,1 为班次,12 为工资。这是它的显示:

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

4

1 回答 1

0

Convert.ToString不会给你一个错误,因为其中一个用 object - 调用它的重载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);

     }


}

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

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

于 2015-12-07T04:01:10.947 回答