Stackoverflow exception in form class

When I try to create a form class object in a form class, it throws an exception as stackoverflow.However, when I declare a form class object inside a method, it works fine. The code is as follows

namespace WindowsFormsApplication6 { public partial class Form1 : Form { **Form1 f1 = new Form1();**//gives stackoverflow exception....... char[] ar = new char[15]; int flag = 0, end; double val1, val2, res; string oprt; public Form1() { InitializeComponent(); } private void masters(object sender, EventArgs e) { ar[i] = char.Parse(((Button)sender).Text); if (char.IsDigit(ar[i])) { if (flag != 0) { if (textBox1.Text == oprt) { textBox1.Clear(); } } else { if (end == 1) { textBox1.Clear(); end = 0; } } Button ansbox = sender as Button; textBox1.Text += ansbox.Text; } else if (char.IsSymbol(ar[i])) { if (textBox1.TextLength != 0) { val1 = double.Parse(textBox1.Text); textBox1.Clear(); Button bt = sender as Button; if (bt != null) textBox1.Text = bt.Text; oprt = bt.Text; // dot.Enabled = true; flag = 1; } } } private void button14_Click(object sender, EventArgs e) { if (textBox1.TextLength != 0) { val2 = double.Parse(textBox1.Text); switch (oprt) { case "+": res = val1 + val2; break; case "-": res = val1 - val2; break; case "*": res = val1 * val2; break; case "/": res = val1 / val2; break; } textBox1.Text = res.ToString(); flag = 0; end = 1; } } } } } 
+6
c # winforms
source share
7 answers

Creating an instance of Form1 will initialize the property f1 with an instance of Form1, which will initialize the property f1 with an instance of Form1, which will initialize the property f1 with an instance of Form1, which will initialize the property f1 with an instance of Form1, which will initialize the property f1 with an instance of Form1, which will lead to initialization of the property f1 by an instance of Form1, which will lead to the initialization of the property f1 with an instance of Form1, which will lead to the initialization of the property f1 by an instance of Form1, which will lead to the initialization of tva f1 by an instance of Form1, which will lead to the initialization of the property f1 by an instance of Form1, which can cause the property f1 to be initialized by an instance of Form1, which will lead to the initialization of the property f1 by an instance of Form1, which will cause the property f1 to be initialized by an instance of Form1, which ... Stack Overflow!

As part of the method in the class, "f1" will be local and exists only for the life of the call. If you do not name the same method on the created instance of Form1, then subsequent Form1s will not be created.

+6
source share

You create a private instance of Form1 when Form1 is created, so this is kind of an infinite loop:

Somewhere in your code, you create your first instance of Form1. When this instance is created, it creates a new instance of Form1. This instance also creates an instance of Form1, and again, and again, etc.

So, when an instance is created, all variables are initialized and when you declare them as follows: Form1 f1 = new Form1() this will automatically activate a new instance of the form.

I suggest that you do not have a new instance of Form1 inside Form1, but if you really need to create a method to instantiate:

Change Form1 f1 = new Form1(); on Form1 f1; . And create a method:

 public void InstantiateNewForm1Instance() { f1 = new Form1(); } 

But: DO NOT CALL THIS METHOD IN THE DESIGNER! Or you will have the same problem :-)

+4
source share

This is because every time you create an instance of your class, it creates another instance of your class, etc ... and so on ... and so on ...

In other words, you have infinite recursion when trying to instantiate your class.

You need to add some kind of control around whether a new instance of Form1 is created at build time. A simple (and not complete) solution would be something like:

 public partial class Form1 : Form { Form1 f1; public Form1() : this(false) public Form1(bool createNewInstance) { if(createNewInstance) f1 = new Form1(); else f1 = null; } } 
+2
source share

The form is created, then it processes the private fields that create an instance of form1, which then creates instances of the private fields (find form1), which then create this object and process the private fields, continuing.

This is why this happens, whereas in a method, a method is executed only when called, without internal initialization.

Why, when in form1 do you need another instance of the same form?

+1
source share

Maybe because Form1 is trying to create an instance of another Form1, which, in turn, is trying to create an instance of another Form1, etc.

+1
source share

If you are in the new Form1 constructor, an instance of Form1 is already being created. So, before he finishes the creation, you create another one, then he does the same. Before your program loads, too many form1s go on the stack ... overflow the stack.

+1
source share

If each class Form1 contains one new instance of the class Form1, you must have an exception when trying to create it.

You can see this more clearly by trying to draw objects on a piece of paper, simulating the space that they occupy in memory and the space occupied by their children - but remember that each shape 1 must be the same size!

0
source share

All Articles