我在我的程序中得到一個堆棧溢出錯誤。我有一個功能和屬性的類,我需要我的部分類與WinForm的文本框,複選框等,才能夠訪問。當我建立它是好的,但我得到一個運行時錯誤。它指向我在Winform中使用的默認構造函數,就像Visual Studio聲稱堆棧溢出的地方一樣。 代碼:堆棧溢出錯誤。使用winforms
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#有點新,所以我不確定問題是什麼。
開始通過查看整個異常信息,包括堆棧跟蹤。 – Blorgbeard 2014-09-29 01:03:33