仍在学习'System.StackOverflowException'被抛出。

时间:2018-11-23 05:52:51

标签: c# .net winforms function class

我已经尝试过if,else if,else语句,我已经尝试过嵌套if语句。我尝试使用变量代替if语句。除团队负责人部分外,所有功能均正常运行,“ TrainHours”存在问题,并且重复使用该代码,更正后的“ Hours”存在问题,最新的更正是“ GetPay()” ”。

我已经尝试了可以​​在网上以及在教科书中找到的各种方法,但是无论我得到什么

“引发了'System.StackOverflowException'。'“

-OR-

“'未知模块异常中的未知错误'”

-对主持人的个人感谢已删除-jeets82(已编辑)

主要形式:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void proWorker_Click(object sender, EventArgs e)
    {
        string pName, pShift;
        decimal pOTime, pPay;

        pName = pNameTxt.Text;


        if (pName != "" && 
            int.TryParse(pNumTxt.Text, out int pNum) && 
            decimal.TryParse(pHRateTxt.Text, out decimal pHRate) &&
            decimal.TryParse(pHoursTxt.Text, out decimal pHours))
        {
                if (dayBtn.Checked)
                {
                    ProductionWorker pWorker1 = new ProductionWorker();
                    pPay = pWorker1.getPay();
                    pOTime = pWorker1.Overtime();

                    pShift = "Day";
                    ProductionWorker pWorker = new ProductionWorker(pName, pNum, pShift, pHRate, pHours, pOTime, pPay);                      

                    MessageBox.Show(pWorker.getData());

                    StreamWriter empFile;
                    empFile = File.AppendText("proWorkerDay.txt");

                    Employee aEmp = new Employee();

                    empFile.WriteLine(pWorker.getProWorkerFile());

                    empFile.Close();
                }

                if (nightBtn.Checked)
                {
                    ProductionWorker pWorker2 = new ProductionWorker();
                    pPay = pWorker2.getPay();
                    pOTime = pWorker2.Overtime();
                    pShift = "Night";

                    ProductionWorker pWorker2N = new ProductionWorker(pName, pNum, pShift, pHRate, pHours, pOTime, pPay);
                    MessageBox.Show(pWorker2N.getData());

                    StreamWriter empFile;
                    empFile = File.AppendText("proWorkerNight.txt");

                    Employee aEmp = new Employee();

                    empFile.WriteLine(pWorker2N.getProWorkerFile());

                    empFile.Close();
                }
        }   

        else
        {
            MessageBox.Show("Enter valid Production worker information");
        }

        pNameTxt.Text = "";
        pNumTxt.Text = "";
        pHRateTxt.Text = "";
        pHoursTxt.Text = "";
    }

    private void addEmp_Click(object sender, EventArgs e)
    {
        string eName;
        int eNum;
        eName = eNameTxt.Text;

        if (Name != "" && int.TryParse(eNumTxt.Text, out eNum))
        {

            Employee employee = new Employee(eName, eNum);

            MessageBox.Show(employee.getData());

            StreamWriter empFile;
            empFile = File.AppendText("EmployeeFile.txt");

            Employee aEmp = new Employee();

            empFile.WriteLine(employee.getEmployeeFile());

            empFile.Close();                                
        }

        else
        {
            MessageBox.Show("Enter valid employee information.");
        }


    }

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

    private void sAddBtn_Click(object sender, EventArgs e)
    {
        string sName, sShift;
        decimal sPay;

        sName = sNameTxt.Text;

        if (sName != "" &&
            int.TryParse(sNumTxt.Text, out int sNum) &&
            decimal.TryParse(sSalTxt.Text, out decimal sSal) &&
            decimal.TryParse(sBonusTxt.Text, out decimal sBonus))
        {

                if (sDayBtn.Checked)
                {
                    ShiftSupervisor sWorker1 = new ShiftSupervisor();
                    sPay = sWorker1.getSPay();                        

                    sShift = "Day";
                    ShiftSupervisor sWorker = new ShiftSupervisor(sName, sNum, sShift, sSal, sBonus, sPay);

                    MessageBox.Show(sWorker.getData());

                    StreamWriter empFile;
                    empFile = File.AppendText("sWorkerDay.txt");

                    Employee aEmp = new Employee();

                    empFile.WriteLine(sWorker.getSWorkerFile());

                    empFile.Close();
                }

                if (sNightBtn.Checked)
                {
                    ShiftSupervisor sWorker2 = new ShiftSupervisor();
                    sPay = sWorker2.getSPay();

                    sShift = "Night";
                    ShiftSupervisor sWorker2N = new ShiftSupervisor(sName, sNum, sShift, sSal, sBonus, sPay);
                    MessageBox.Show(sWorker2N.getData());

                    StreamWriter empFile;
                    empFile = File.AppendText("sWorkerNight.txt");

                    Employee aEmp = new Employee();

                    empFile.WriteLine(sWorker2N.getSWorkerFile());

                    empFile.Close();
                }

        }

        else
        {
            MessageBox.Show("Enter valid Shift Supervisor information.");
        }

        sNameTxt.Text = "";
        sNumTxt.Text = "";
        sSalTxt.Text = "";
        sBonusTxt.Text = "";
    }

    private void addTLBtn_Click(object sender, EventArgs e)
    {
        string tName, tShift;
        decimal tOTime, tPay;

        tName = tNameTxt.Text;


        if (tName != "" &&
            int.TryParse(tNumTxt.Text, out int tNum) &&
            decimal.TryParse(tHRateTxt.Text, out decimal tHRate) &&
            decimal.TryParse(tHoursTxt.Text, out decimal tHours) &&
            decimal.TryParse(tTrainTxt.Text, out decimal tTrain))
        {
            if (tDayBtn.Checked)
            {
                TeamLeader tWorker1 = new TeamLeader();
                tPay = tWorker1.getTPay();
                tOTime = tWorker1.Overtime();
                decimal rHours = tWorker1.ReqHours;
                decimal tTHours = tWorker1.TrainHours;

                tShift = "Day";
                TeamLeader tWorker = new TeamLeader(tName, tNum, tShift, tHRate, tHours, tOTime, rHours, tTHours, tPay);

                MessageBox.Show(tWorker.getData());

                StreamWriter empFile;
                empFile = File.AppendText("TeamLeaderDay.txt");

                Employee aEmp = new Employee();

                empFile.WriteLine(tWorker.getTeamLeaderFile());

                empFile.Close();
            }

            if (tNightBtn.Checked)
            {
                TeamLeader tWorker2 = new TeamLeader();
                tPay = tWorker2.getTPay();
                tOTime = tWorker2.Overtime();
                decimal rHours = tWorker2.ReqHours;
                decimal tTHours = tWorker2.TrainHours;

                tShift = "Day";
                TeamLeader tWorker2N = new TeamLeader(tName, tNum, tShift, tHRate, tHours, tOTime, rHours, tTHours, tPay);
                MessageBox.Show(tWorker2N.getData());

                StreamWriter empFile;
                empFile = File.AppendText("TeamLeaderNight.txt");

                Employee aEmp = new Employee();

                empFile.WriteLine(tWorker2N.getTeamLeaderFile());

                empFile.Close();
            }
        }

        else
        {
            MessageBox.Show("Enter valid Production worker information");
        }

        tNameTxt.Text = "";
        tNumTxt.Text = "";
        tHRateTxt.Text = "";
        tHoursTxt.Text = "";
        tTrainTxt.Text = "";
    }
}

基类:

class Employee
{
    private string _name;
    private int _number;
    private string _empAddList;

    public Employee(string name, int num)
    {
        _name = name;
        _number = num;
        _empAddList = name + " " + num.ToString();
    }

    public Employee()
    {
        _name = "";
        _number = 0;
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public int Number
    {
        get { return _number; }
        set { _number = value; }
    }

    public string EmpAddList
    {
        get { return _empAddList; }
        set { _empAddList = value; }
    }

    //define method to return the values
    public virtual string getData()
    {
        string line = "";
        line += " Name: \t\t" + this.Name + "\n";
        line += " Number: \t" + this.Number;
        return line;
    }

    public string getEmployeeFile()
    {
        string line = "";
        line += this.Name + "," + this.Number;
        return line;
    }

}

abstract class Employee1 : Employee
{
    public abstract decimal Overtime();
    public abstract decimal getPay();
}

问题类 我已经修复了几乎每个部分,每次修复时,它都会给我带来不同的错误或StackOverflowException。

class TeamLeader : Employee
{
    private string _shiftNum;
    private decimal _hourlyRate;
    private decimal _reqHours;
    private decimal _trainHours;
    private decimal _hours;
    private decimal _overtime;
    private decimal _oT;


    public string ShiftNum
    {
        get { return _shiftNum; }
        set
        {
            if (_shiftNum == "Day Shift")
            {
                _shiftNum = "Day";
            }

            else
            {
                _shiftNum = "Night";
            }
        }
    }

    public decimal HourlyRate
    {
        get { return _hourlyRate; }
        set { _hourlyRate = value; }
    }

    public decimal Hours
    {
        get { return _hours; }
        set { _hours = value; }
    }

    public decimal ReqHours
    {
        get { return _reqHours; }
        set { _reqHours = value; }
    }

    public decimal TrainHours
    {
        get { return _trainHours; }
        set { _trainHours = value; }
    }

    public decimal Overtime()
    {
        decimal oTime = 0;

        if (Hours > 40)
        {
            _oT = Hours - 40;
            oTime = (HourlyRate * 1.5m) * _oT;
        }

        else
        {
            oTime = 0;
        }

        return oTime;
    }

    public decimal getTBonus()
    {
        decimal tBonus = 0;            
        if (TrainHours <= 0 && Hours <= 0)
        { tBonus = 0; }

        if (TrainHours >= (Hours * .15m))
        { tBonus = getTPay() * .15m; }

        if (TrainHours >= (Hours * .1m))
        { tBonus = getTPay() * .125m; }

        if (TrainHours >= (Hours * .08m))
        { tBonus = getTPay() * .05m; }

        else
        { tBonus = getTPay() * .025m; }

        return tBonus;
    }

    public decimal getTTrainHours()
    {
        decimal tHours = 0;
        if (TrainHours <= 0 && Hours <= 0)
        { tHours = 0; }

        if (TrainHours >= (Hours * .15m))
        { tHours = Hours * .15m; }

        else if (TrainHours <= (Hours * .1m))
        { tHours = Hours * .125m; }

        else if (TrainHours <= (Hours * .08m))
        { tHours = Hours * .05m; }

        else
        { tHours = Hours * .025m; }

        return tHours;
    }

    public decimal getTPay()
    {
        decimal paid = 0;

        if (Hours <= 0)
        { paid = 0; }

        if (Hours > 40)
        { paid = (HourlyRate * 40) + Overtime() + getTBonus(); }

        else
        { paid = (Hours * HourlyRate) + getTBonus(); }

        return paid;
    }

    public TeamLeader(string name, int num, string shift, decimal hr, decimal hours, decimal oTime, decimal rHours, decimal tTHours, decimal pay) : base(name, num)
    {
        _shiftNum = shift;
        _hourlyRate = hr;
        _hours = hours;
        _overtime = oTime;
        _trainHours = rHours;
        _reqHours = tTHours;

    }

    public TeamLeader()
    {
        _shiftNum = "";
        _hourlyRate = 0;
        _hours = 0;
        _overtime = 0;
        _trainHours = 0;
        _reqHours = 0;
    }

    public override string getData()
    {
        string line = "";
        line += " Name: \t" + this.Name + "\n";
        line += " Number: \t" + this.Number + "\n";
        line += " Shift Number: \t" + this.ShiftNum + "\n";
        line += " Hourly Rate: \t" + this.HourlyRate.ToString("C") + "\n";
        line += " Hours Worked: \t" + this.Hours + "\n";
        line += " Training Hours: \t" + this.TrainHours.ToString() + "\n";
        line += " Overtime Hours: \t" + (this.Hours - 40) + "\n";
        line += " Overtime Pay: \t" + this.Overtime().ToString("C") + "\n";            
        line += " Bonus Multiplier: \t" + this.getTTrainHours().ToString("P") + "\n";
        line += " Bonus: \t" + this.getTBonus().ToString("C") + "\n";
        line += " Weekly Pay: \t" + this.getTPay().ToString("C");

        return line;
    }

    public string getTeamLeaderFile()
    {
        string line = "";
        line += this.Name + "," + this.Number + "," + this.ShiftNum + "," + this.HourlyRate.ToString() + "," + this.Hours + "," + this.TrainHours.ToString() + "," + (this.Hours - 40) + "," + 
            this.Overtime().ToString() + "," + this.getTTrainHours().ToString("P") + "," + this.getTBonus().ToString("C") + "," + this.getTPay().ToString();
        return line;
    }
}

}

2 个答案:

答案 0 :(得分:4)

问题出在TeamLeader::getData()

getTBonus()调用getTPay(),后者再次调用getTBonus()导致无限循环,这将引发StackOverflowException

您可以尝试在这些方法中使用if...else if,而不仅仅是if

public decimal getTBonus()
{
    decimal tBonus = 0;
    if (TrainHours <= 0 && Hours <= 0)
    { tBonus = 0; }

    else if (TrainHours >= (Hours * .15m))
    { tBonus = getTPay() * .15m; }

    else if (TrainHours >= (Hours * .1m))
    { tBonus = getTPay() * .125m; }

    else if (TrainHours >= (Hours * .08m))
    { tBonus = getTPay() * .05m; }

    else
    { tBonus = getTPay() * .025m; }

    return tBonus;
}

public decimal getTPay()
{
    decimal paid = 0;

    if (Hours <= 0)
    { paid = 0; }

    else if (Hours > 40)
    { paid = (HourlyRate * 40) + Overtime() + getTBonus(); }

    else
    { paid = (Hours * HourlyRate) + getTBonus(); }

    return paid;
}

答案 1 :(得分:0)

我能够通过从getPay()方法中删除getBonus()解决问题。完成之后,我添加了一个新变量,将它们添加到getData()方法中。这工作了。现在,我只需要弄清楚为什么我的TrainHours不会填充。谢谢您,这对指导我给出答案很有帮助。

十进制每周支付= getTBonus()+ getTPay();

line + =“每周工资:\ t” + weeklyPay.ToString(“ C”);