堆栈溢出错误.使用winforms

本文关键字:winforms 使用 错误 栈溢出 堆栈 | 更新日期: 2023-09-27 18:24:49

我的程序中出现堆栈溢出错误。我有一个带有函数和属性的类,我需要我的带有winform文本框、复选框等的分部类才能访问。当我建立它是好的,但我得到一个运行时错误。它指向我在winform中使用的默认构造函数,以及VisualStudio声明堆栈溢出的位置。代码:

public class TeamCreator:Form
    {
        //fields
        public string[] players=new string[12];
        public int[] points=new int[12];
        public int currentPlayerScore=0;
        public TeamCreator()//default constructor
        {
            for (int i = 0; i < 11; i++)
            {
                this.players[i] = "";
                this.points[i] = 0;
            }
        }
        public TeamCreator(string[] teammates,int[] scores)//construct Team object with user input
        {
            this.players = teammates;
            this.points = scores;
        }
        public void setTeammates(string player,int index)//set players array
        {
            this.players[index] = player;
        }
        public void setPoints(int[] scoreList)//set points array
        {
                this.points = scoreList;
        }
        public void setPlayerScore(int playerScore,int playerNum)//sets a specific player's score
        {
            this.points[playerNum] = playerScore;
        }
        public int[] getPoints()//obtain array of points
        {
            int[] listOfPoints=new int[12];
            int i;
            for(i=0;i<11;i++)
            {
                listOfPoints[i]=this.points[i];
            }
            return listOfPoints;
        }
        public int totalPoints()//gets total points
        {
            int total=0;
            for(int i=0;i<11;i++)
            {
                total = this.points[i] + total;
            }
            return total;
        }
        public double meanPoints()//returns mean or average of total points
        {
            int total = this.totalPoints();
            int mean = total / 11;
            return mean;
        }
    }
}
              //winform code
              namespace TeamClass
{
    public partial class TeamClass:TeamCreator
    {
        public int indexOn = 0;
        public int current = 0;
        public TeamCreator newTeam = new TeamCreator();
        public TeamClass()
        {
            InitializeComponent();
        }
        private void playerInput_MouseLeave(object sender, EventArgs e)//adds players to players array and to list
        {
            string playerName = playerInput.Text;
            newTeam.setTeammates(playerName,this.indexOn);
            playerList.Items.Add(playerName);
            indexOn++;
        }
        TeamClass reopen = new TeamClass();
        private void restart_CheckedChanged(object sender, EventArgs e)//allows user to restart program and make a new team
        {
            this.Visible = false;
            reopen.Show();
        }
        private void playerScoreDisplay_CheckedChanged(object sender, EventArgs e)//displays currently selected player when checked
        {
            string currentPlayerSelected = newTeam.players[current];
            MessageBox.Show("The current player selected is " + currentPlayerSelected + ".", "Current Player", MessageBoxButtons.OK);
        }
        private void playerList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.current = playerList.SelectedIndex;

        }
        private void scoreInput_MouseLeave(object sender, EventArgs e)//gets player score
        {
            int currentScore;
            if (!Int32.TryParse(scoreInput.Text, out currentScore))
            {
                //tell user we can't parse the amount
                if (MessageBox.Show("Text did not parse to an integer, please try again", "Invalid Argument", MessageBoxButtons.RetryCancel) == System.Windows.Forms.DialogResult.Cancel)
                {
                    // leave method if they don't want to try again
                    return;
                }
                else
                {
                    //set the focus on the control so user can fix error
                    scoreInput.Focus();
                    //As a convenience select all text
                    scoreInput.SelectAll();
                    //exit method
                    return;
                }
            }
                newTeam.setPlayerScore(currentScore, current);

        }
        private void scoreInput_KeyPress(object sender, KeyPressEventArgs e)//makes sure numbers are being entered correctly for score
        {
            //only accept negative sign in first position
            if ((e.KeyChar == '-') && ((sender as TextBox).Text.Length == 0))
            {
                if ((sender as TextBox).Text.Contains("-"))
                    e.Handled = true;
            }
            //Only accept numbers, one decimal, one negative sign (-) and the backspace
            else if (!char.IsDigit(e.KeyChar) && !char.IsPunctuation(e.KeyChar) && !(e.KeyChar == 0x08))
            {
                e.Handled = true;
            }
        }
        private void totalPointsDisplay_CheckedChanged(object sender, EventArgs e)//displays total points when checked
        {
            int total=newTeam.totalPoints();
            MessageBox.Show("The total points for the team is " + total + ".", "Total Points", MessageBoxButtons.OK);
        }
        private void MeanPointsDisplay_CheckedChanged(object sender, EventArgs e)//displays mean points when checked
        { 
            double avg=newTeam.meanPoints();
            MessageBox.Show("The mean points for the team is " + avg + ".", "Mean Points", MessageBoxButtons.OK);
        }
    }
  }

对于我为什么会出现这个错误以及如何修复它,我们将不胜感激。我对C#有点陌生,所以不确定问题出在哪里。

堆栈溢出错误.使用winforms

不幸的是,您可能无法从异常中得到太多信息。您将看到这样的消息,而不是有用的堆栈跟踪:

无法计算表达式,因为当前线程处于堆栈溢出状态

如果你搜索"stackoverflow异常堆栈跟踪",有一些方法可以解决这个问题。

不过,根本问题几乎总是递归,所以要寻找调用自己的东西,或者在循环中寻找两到三个相互调用的方法。

在快速扫描您的代码后,我几乎立即找到了一个(可能还有其他代码)。每次实例化TeamClass时,您都在创建另一个实例,该实例会创建另一实例,并且不断

public partial class TeamClass:TeamCreator
{
    ...
    TeamClass reopen = new TeamClass();
    ...

在所有代码中使用数组中的前11个元素有什么原因吗?你定义了一个有12个元素的数组,但你总是循环到第11个元素

 public string[] players=new string[12];
        public int[] points=new int[12];
        public int currentPlayerScore=0;
        public TeamCreator()//default constructor
        {
            here why you loop to 11 it suppose to be 12 or the array.Length
            for (int i = 0; i < 11; i++)
            {
                this.players[i] = "";
                this.points[i] = 0;
            }