我在从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();
}
}
}
}
我的Employee方法如下所示:
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 :(得分:2)
您的问题似乎是EmployeeData
集合的类型为string
,而不是Employee
,从而丢失了您的类实例的所有结构信息。您需要更改集合的类型,以及以下行:
EmployeeData[count++] = Convert.ToString(new Employee(num, str, str2, num2, num3));
...为:
EmployeeData[count++] = new Employee(num, str, str2, num2, num3);