2012-12-08 45 views
-1

我有一個問題,從我的Employee類調用方法,我的代碼如下,任何建議表示讚賞。調用另一個類的方法問題

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

    private void openToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     int num = 0; 
     string str = null; 
     string str2 = null; 
     double num2 = 0.0; 
     double num3 = 0.0; 

     //code to allow a file to be browed for and opened 
     Stream myStream = null; 
     OpenFileDialog openFileDialog1 = new OpenFileDialog(); 

     openFileDialog1.InitialDirectory = "c:\\"; 
     openFileDialog1.Filter = "text files (*.txt)|*txt"; 

     if (openFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      if ((myStream = openFileDialog1.OpenFile()) != null) 
      { 
       //the streamreader is named 'data' 
       data = new StreamReader(myStream); 

       string fileData = ""; 
       while (fileData != null) 
       { 
        fileData = data.ReadLine(); 
        if (fileData != null) 
        { 
         num = int.Parse(fileData); 
         str = data.ReadLine(); 
         str2 = data.ReadLine(); 
         string[] strArray = data.ReadLine().Split(new char[0]); 
         num3 = double.Parse(strArray[0]); 
         num2 = double.Parse(strArray[1]); 
         EmployeeData[count++] = Convert.ToString(new Employee(num, str, str2, num2, num3)); 
        } 
       } 
       NextBtn.Enabled = true; 
       numEmployees = count; 
       count = 0; 
       nameTxtBox.Text = EmployeeData[count].GetName(); 
       addressTxtBox.Text = Convert.ToString(EmployeeData[count].GetAddress()); 
       string str4 = string.Format("{0:C}", EmployeeData[count].calcPay()); 
       payTxtBox.Text = str4; 
      } 
     } 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     count++; 
     if (count < this.numEmployees) 
     { 
      nameTxtBox.Text = EmployeeData[count].GetName(); 
      addressTxtBox.Text = EmployeeData[count].GetAddress(); 
      string str = string.Format("{0:C}", EmployeeData[count].calcPay()); 
      payTxtBox.Text = str; 
     } 
     else 
     { 
      NextBtn.Enabled = false; 
      nameTxtBox.Clear(); 
      addressTxtBox.Clear(); 
      payTxtBox.Clear(); 
     } 

    } 


} 
} 

我的員工的方法是這樣的:

public class Employee 
{ 
    private string employeeAddress; 
    private string employeeName; 
    private int employeeNumber; 
    private const double FED_TAX = 0.2; 
    private const int FULL_TIME = 40; 
    private double hourlyWage; 
    private double hoursWorked; 
    private const double OVER_TIME = 1.5; 
    private const double STATE_TAX = 0.075; 

    public Employee() 
    { 
     employeeNumber = 0; 
     employeeName = ""; 
     employeeAddress = ""; 
     hourlyWage = 0.0; 
     hoursWorked = 0.0; 
    } 

    public Employee(int _num, string _name, string _address, double _hours, double _wage) 
    { 
     employeeNumber = _num; 
     employeeName = _name; 
     employeeAddress = _address; 
     hourlyWage = _wage; 
     hoursWorked = _hours; 
    } 

    public double calcPay() 
    { 
     double pay = hoursWorked * hourlyWage; 
     double ot = hoursWorked - FULL_TIME; 
     if (ot > 0.0) 
     { 
      pay += ot * (hourlyWage * OVER_TIME); 
     } 
     double tax1 = pay * FED_TAX; 
     double tax2 = pay * STATE_TAX; 
     return ((pay - tax1) - tax2); 
    } 

    public string GetAddress() 
    { 
     return employeeAddress; 
    } 

    public double GetHours() 
    { 
     return hoursWorked; 
    } 

    public string GetName() 
    { 
     return employeeName; 
    } 

    public double GetWage() 
    { 
     return hourlyWage; 
    } 
} 
} 
+0

什麼問題您有? – Maciej

+0

你的問題是什麼? – Silvermind

+0

我收到此錯誤:錯誤「串」不包含「的getAddress」,沒有擴展方法「的getAddress」接受型「字符串」的第一個參數的定義可以找到(是否缺少using指令或一個程序集引用?) – BXL

回答

2

您的問題似乎是,你EmployeeData收集string型的,而不是Employee,從而失去所有的結構信息,爲你的類的實例。您需要更改集合類型,以及從以下線路:

EmployeeData[count++] = Convert.ToString(new Employee(num, str, str2, num2, num3)); 

...到:

EmployeeData[count++] = new Employee(num, str, str2, num2, num3); 
+0

謝謝,就是這樣! – BXL