I am having a problem calling a method from my Employee class, my code is below, any suggestions are appreciated.
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();
}
}
}
}
my Employee method looks like this:
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;
}
}
}
Your issue appears to be that your
EmployeeDatacollection is of typestring, rather thanEmployee, thereby losing all structural information for your class instances. You need to change the type of the collection, as well as the following line from:…to: