2015-12-07 58 views
1

我不完全確定標題對此很有幫助。我被困在學校的任務上,假設是一個視頻來展示如何做到這一點,但對於我的生活,我無法弄清楚如何從皮爾森網站下載學生文件。以下是我正在處理的問題。員工和生產工人班,從班級獲取信息

員工和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的是在那裏,因爲我無法弄清楚如何使用移位代碼或多或少地使用它作爲佔位符,直到我指出它。我不知道如何解決這個問題,希望它有一個小小的錯誤。

多虧了我能解決的第一個問題我有,現在我在與末尾的消息框出現問題的commets幫助。我輸入的信息是,名稱爲Glitter,ID號碼爲12,輪班號碼爲1,付費號碼爲12。下面是它的表現:

名稱System.Windows.Forms.TextBox,文本:GlitterIDNumber 0Hourly率 System.Windows.Forms.TextBox,文字:按住Shift鍵SystemWindowsForm.TextBox,文本:

+0

它應該是'Convert.ToInt32(txtIdNumb.Text)'。你想轉換文本屬性,而不是文本框本身。 –

+0

啊,謝謝羅恩。 – Lyght

+0

我的留言箱無法正常工作/顯示正確,現在正試圖解決此問題。 – Lyght

回答

0

Convert.ToString犯規給你錯誤,因爲它之一調用它的超載與對象 - public static string ToString(object value)。但是,由於您對用戶輸入的值感興趣,請使用 - TextBox.Text屬性,而不是通過TextBox

更新1:有關此問題的

System.Convert.ToString(object value)一些內部在如下實施。網 -

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

    } 


} 

我已添加評論來詳細說明我所做的所有更改,如果您有任何問題,請寫信給我評論。

另外,此時您的代碼不會驗證文本框中的用戶輸入。