Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仍在学习';System.StackOverflowException';被抛出;_C#_.net_Winforms_Function_Class - Fatal编程技术网

C# 仍在学习';System.StackOverflowException';被抛出;

C# 仍在学习';System.StackOverflowException';被抛出;,c#,.net,winforms,function,class,C#,.net,Winforms,Function,Class,我试过if,else-if,else语句,我试过嵌套if语句。我试着用变量代替if语句。除了团队负责人部分之外,所有功能都正常工作,“TrainHours”有一个问题,并且重复使用它,在更正之后,“Hours”有一个问题,最新的更正是“GetPay() 我已经尝试了各种不同的方法,我可以在网上找到,在我的教科书,但无论我做什么,我得到 “'System.StackOverflowException'被抛出。” -或 “‘未知模块异常中的未知错误’” -删除对版主的个人感谢-jeets82(已编辑

我试过if,else-if,else语句,我试过嵌套if语句。我试着用变量代替if语句。除了团队负责人部分之外,所有功能都正常工作,“TrainHours”有一个问题,并且重复使用它,在更正之后,“Hours”有一个问题,最新的更正是“GetPay()

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

“'System.StackOverflowException'被抛出。”

-或

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

-删除对版主的个人感谢-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;
    }
}
班组长:员工
{
私有字符串_shiftNum;
私有十进制_hourlyRate;
专用十进制时间;
私人十进制列车小时数;
私人十进制小时数;
私人十进制超时;
私有十进制;
公共字符串移位
{
获取{return\u shiftNum;}
设置
{
如果(_shiftNum==“白班”)
{
_shiftNum=“日”;
}
其他的
{
_shiftNum=“夜间”;
}
}
}
公共十进制小时率
{
获取{return\u hourlyRate;}
设置{u hourlyRate=value;}
}
公共十进制小时数
{
获取{return\u hours;}
设置{u小时=值;}
}
公共服务时间
{
获取{return\u reqHours;}
设置{u reqHours=value;}
}
公共十进制列车小时数
{
获取{return\u trainHours;}
设置{u trainHours=value;}
}
公共十进制超时()
{
十进制时间=0;
如果(小时数>40)
{
_oT=小时-40;
时间=(小时速率*1.5m)*\u oT;
}
其他的
{
oTime=0;
}
返回时间;
}
公共奖金()
{
十进制tBonus=0;
如果(列车小时数=(小时数*.1m))
{tBonus=getTPay()*.125m;}
如果(列车小时数>=(小时数*.08m))
{tBonus=getTPay()*.05m;}
其他的
{tBonus=getTPay()*.025m;}
返回tBonus;
}
公共小火车小时数()
{
十进制tHours=0;

如果(列车时数问题出在
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;
}
public奖金()
{
十进制tBonus=0;
如果(列车小时数=(小时数*.1m))
{tBonus=getTPay()*.125m;}
否则,如果(列车小时数>=(小时数*.08m))
{tBonus=getTPay()*.05m;}
其他的
{tBonus=getTPay()*.025m;}
返回tBonus;
}
公共支付
{
十进制支付=0;
若有(40小时)
{paid=(HourlyRate*40)+加班()+getbonus();}
其他的
{已付=(小时*小时费率)+getTBonus();}
已支付的回报;
}

我可以通过从getPay()方法中删除getBonus()来解决这个问题。之后,我添加了一个新变量,将它们添加到getData()方法中。这很有效。现在我只需要找出我的TrainHours无法填充的原因。感谢所有帮助我找到答案的内容

decimal weeklyPay=getTBonus()+getTPay();


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

欢迎来到StackOverflow。这里有一个笑话,讲的是一台称体重的机器,一个大个子站在上面,上面写着“请一次一个人!”。这个问题有点像。你的问题需要集中在一个问题上。你需要提供(例如,你的问题是删除所有已注释的代码)。选择一个例外,并在发生时向我们显示代码;并告诉我们它发生在哪一行。我赞同背景信息,但这与问题无关:试着关注问题。“如何提问”使用短语“与忙碌的同事交谈”。我没有否决投票。我只是想帮你。你提到了两个例外,而不仅仅是一个。如果你不学习,你在这里的体验会很糟糕。例如,让我提供一个无关的提示:
StreamWriter
是可识别的,所以应该在
使用
块中。一旦你这样做了,你就不需要关闭它,因为它将在我想澄清和道歉。我把这部分内容指向@Uwe Keim,他在没有解释的情况下否决了我的问题,并删除了我表示的先发制人的赞赏。你对如何更好地表达我的问题有什么指导吗?我很乐意尝试更直接。没问题:正如你所说的那样f、 我们都去过那里,很高兴你找到了答案。不要把反对票放在心上——StackOverflow是一个强大的工具;但你必须学会如何使用它。在“如何提问”页面上有很多提示,MCVE页面经常被引用,适用于这里:在StackOverflow上抛出大量代码并说“帮助”太容易了,但这不是一个好问题,我担心这个问题偏离了太多的方向,这可能会降低投票率。请访问meta.stackoverflow.com了解人们如何使用此网站。最好wishes@Richardissimo我想谢谢你。这花了一些时间。事实上又花了一整天,但我学会了使用:FileStream和:用以实现IDisposable。谢谢。无限循环m