int number = 1;
int maxnumber = 10000;
void LoadFavorites()
{
if (number <= maxnumber)
{
if (Properties.Settings.Default.FavoriteList.Contains("'"+number+"'"))
{
this.listBox1.Items.Add(number);
}
}
// Increases number by 1 and reruns
number = number + 1;
LoadFavorites(); //**MOVE THESE:
}
Change to
int number = 1;
int maxnumber = 10000;
void LoadFavorites()
{
if (number <= maxnumber)
{
if (Properties.Settings.Default.FavoriteList.Contains("'"+number+"'"))
{
this.listBox1.Items.Add(number);
// Increases number by 1 and reruns
number = number + 1;
LoadFavorites(); // problem is probably here
}
}
}
You're calling a function from within a function, which, as you probably know, is recursion. With recursion, however, you have to have an exit condition, which causes the loop to exit.
The stack is the order of function calls in a thread. For example, in Java, you have main() which is your entry point. Calling functions from main() adds to the top of the stack. Once the stack gets to a certain size, your computer can no longer buffer more function calls. It simply drops the main function from the stack, and doesn't know where to return to, so it throws an error. (It throws the error once 'main' is dropped, not once it tries to return).
The reason for the change is that you want to continue only if number is < maxnumber. This sets up a condition for the recursion, after which, the function will exit.