堆栈溢出错误。使用winforms

时间:2014-09-29 00:57:12

标签: c# winforms stack-overflow

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

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#不熟悉,所以不确定是什么问题。

2 个答案:

答案 0 :(得分:2)

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

  

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

如果您搜索“stackoverflow exception stack trace”,有很多方法可以解决这个问题。

根问题几乎总是递归,所以寻找调用自身的东西,或者循环调用另外两个或三个方法。

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

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

答案 1 :(得分:0)

您在所有代码中使用数组中的前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;
            }