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

    }


 }
}

私の 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;
    }
}
}
4

1 に答える 1

2

あなたの問題は、EmployeeDataコレクションのタイプstringが ではなく でEmployeeあるため、クラス インスタンスのすべての構造情報が失われているようです。コレクションのタイプと、次の行を変更する必要があります。

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

...に:

 EmployeeData[count++] = new Employee(num, str, str2, num2, num3);
于 2012-12-08T14:44:10.997 に答える