Click here to Skip to main content
15,877,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

When I try to create object of a form, I got the above error during run.
An unhandled exception of type 'System.StackOverflowException' occurred in Test.exe
public Form5 fg = new Form5();
phil.o 19-Aug-15 5:51am    
Please improve your question and show us the code in the Form5 class constructor.
DamithSL 19-Aug-15 5:51am    
what is the full exception?
F-ES Sitecore 19-Aug-15 6:08am    
Your code is in an infinite loop, you'll need to step through the code using the debugger to try and find where the recursion is happening, as already said we can't step through the code for you. A common cause of this is code like

private string myVar;

public string MyVar
return MyVar; // typo here, it should be myVar
phil.o 19-Aug-15 6:12am    
Your example would trigger an error at compile time, not run time.
F-ES Sitecore 19-Aug-15 6:15am    
Nope, it's perfectly valid. I've generated many SO situations myself from this kind of fat-fingered programming.

1 solution

A stack overflow error normally occurs when you have direct or indirect recursion:
So look at your Form5 constructor and see what it does. If it doesn't do much, then start looking at the Form5 fields and properties - somewhere, the class is calling a method that causes the Form5 instance to be created again, or a property setter is causing the property value to be changed.

If it's not obvious, then put a breakpoint on the first line of the constructor - if it gets hit, then you need to start stepping through the constructor code looking for the recursion, if it isn't, then it's probably a field initialisation that is doing it.

We can't do that for you - we can't run your code!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900