I keep getting a stackOverflow error (no pun intended):" An unhandled exception of type 'System.StackOverflowException' occurred in NumberGuessingGame.exe "
I'm guessing it would help if you saw my code and alswo where its ocurring:
Source - testClass:
namespace NumberGuessingGame_08029490
{
public class testClass : Form1
{
public bool _gameWon;
public bool _gameRunning;
public int _number;
public int _guessesRemaining;
public int guessesRemaining
{
get { return _guessesRemaining; }
}
public bool gameEnded
{
get { return !_gameRunning; }
}
public bool gameWon
{
get { return _gameWon; }
}
public testClass()
{
_gameRunning = false;
_gameWon = false;
}
public void saveNewTestGame(int numberGuesses)
{
if (numberGuessesComboBox.SelectedIndex == 0)
{
numberGuesses = 1;
}
if (numberGuessesComboBox.SelectedIndex == 1)
{
numberGuesses = 3;
}
if (numberGuessesComboBox.SelectedIndex == 2)
{
numberGuesses = 5;
}
_guessesRemaining = numberGuesses;
_gameRunning = true;
}
public bool makeGuess(int guessNumber)
{
if (_gameRunning)
{
_guessesRemaining--;
if (_guessesRemaining <= 0)
{
_gameWon = false;
_gameRunning = false;
return false;
}
if (guessNumber == _number)
{
_gameWon = true;
return true;
}
if (guessNumber > _number)
{
guessResultTextBox.Text = "Your Guess is too high, try again";
_gameWon = false;
return false;
}
if (guessNumber < _number)
{
guessResultTextBox.Text = "Your Guess is too low, try again";
_gameWon = false;
return false;
}
else
{
return false;
}
}
else
{
throw new Exception("The game is not running. Call newGame() before making a guess.");
}
}
}
}
Source - gameClass:
namespace NumberGuessingGame_08029490
{
public class gameClass : Form1
{
public bool _gameWon;
public bool _gameRunning;
public static Random randomNum = new Random();
public int _number;
public int _guessesRemaining;
public int guessesRemaining
{
get { return _guessesRemaining; }
}
public bool gameEnded
{
get { return !_gameRunning; }
}
public bool gameWon
{
get { return _gameWon; }
}
public gameClass()
{
_gameRunning = false;
_gameWon = false;
}
public void saveNewGame(int numberGuesses)
{
if (numberGuessesComboBox.SelectedIndex == 0)
{
numberGuesses = 1;
}
if (numberGuessesComboBox.SelectedIndex == 1)
{
numberGuesses = 3;
}
if (numberGuessesComboBox.SelectedIndex == 2)
{
numberGuesses = 5;
}
if (rangeNumbersComboBox.SelectedIndex == 0)
{
int randomNumFive = randomNum.Next(1, 5);
randomNumFive = _number;
}
if (rangeNumbersComboBox.SelectedIndex == 1)
{
int randomNumTen = randomNum.Next(1, 10);
randomNumTen = _number;
}
if (rangeNumbersComboBox.SelectedIndex == 2)
{
int randomNumTwenty = randomNum.Next(1, 20);
randomNumTwenty = _number;
}
_guessesRemaining = numberGuesses;
_gameRunning = true;
}
public bool makeGuess(int guessNumber)
{
if (_gameRunning)
{
_guessesRemaining--;
if (_guessesRemaining <= 0)
{
_gameWon = false;
_gameRunning = false;
return false;
}
if (guessNumber == _number)
{
_gameWon = true;
return true;
}
if (guessNumber > _number)
{
guessResultTextBox.Text = "Your Guess is too high, try again";
_gameWon = false;
return false;
}
if (guessNumber < _number)
{
guessResultTextBox.Text = "Your Guess is too low, try again";
_gameWon = false;
return false;
}
else
{
return false;
}
}
else
{
throw new Exception("The game is not running. Call newGame() before making a guess.");
}
}
}
}
Source - form1:
namespace NumberGuessingGame_08029490
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
testClass newTest = new testClass();
gameClass newGame = new gameClass();
private void saveButton_Click(object sender, EventArgs e)
{
if (testCheckBox.Checked == true)
{
int numberGuesses = Convert.ToInt32(numberGuessesComboBox.SelectedIndex);
int _number = Convert.ToInt32(testNumberTextBox.Text);
newTest.saveNewTestGame(numberGuesses);
}
if (testCheckBox.Checked == false)
{
int numberGuesses = Convert.ToInt32(numberGuessesComboBox.SelectedIndex);
int _number = Convert.ToInt32(rangeNumbersComboBox.SelectedIndex);
newGame.saveNewGame(numberGuesses);
}
}
private void guessButton_Click(object sender, EventArgs e)
{
if (testCheckBox.Checked == true)
{
int guessNumber = Convert.ToInt32(guessNumberTextBox.Text);
bool correctAnswer = newTest.makeGuess(guessNumber);
if (correctAnswer)
{
MessageBox.Show("Weldone, you Won!!");
}
// if (game.GameEnded)
// {
// disable guess button, show loss label
// }
}
if (testCheckBox.Checked == false)
{
int guessNumber = Convert.ToInt32(guessNumberTextBox.Text);
bool correctAnswer = newGame.makeGuess(guessNumber);
if (correctAnswer)
{
MessageBox.Show("Weldone, you Won!!");
}
// if (game.GameEnded)
// {
// disable guess button, show loss label
// }
}
}
}
}
EDIT: No more StackOverflowException, code errors:
Thank you everyone for your help <3
Having removed the "Form 1" from the top It seems to have solved the issue for now, but now that the combobox, textbox variables are no longer inheriting I cant use them in the code, so I can't test to see if the solution has worked, any ideas as to how they could be used?
Error 1 The name 'numberGuessesComboBox' does not exist in the current context E:\Projects\NumberGuessingGame\NumberGuessingGame\testClass.cs 46 17 NumberGuessingGame
Error 7 The name 'rangeNumbersComboBox' does not exist in the current context E:\Projects\NumberGuessingGame_08029490\NumberGuessingGame_08029490\gameClass.cs 58 17 NumberGuessingGame_08029490
Error 10 The name 'guessResultTextBox' does not exist in the current context E:\Projects\NumberGuessingGame_08029490\NumberGuessingGame_08029490\testClass.cs 84 21 NumberGuessingGame_08029490