When I try to create an object of the form class within the form class, it gives an exception as stackoverflow occured.However, when I declare the object of form class within 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;
}
}
}
}
}
NET Framework 2.0, you can't catch a StackOverflowException object with a try / catch block, and the corresponding process is terminated by default. Consequently, you should write your code to detect and prevent a stack overflow.
StackOverflowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion. So make sure your code doesn't have an infinite loop or infinite recursion. StackOverflowException uses the HRESULT COR_E_STACKOVERFLOW, which has the value 0x800703E9.
A StackOverflowException is thrown when the execution stack overflows because it contains too many nested method calls. For example, suppose you have an app as follows: C# Copy. using System; namespace temp { class Program { static void Main(string[] args) { Main(args); // Oops, this recursion won't stop. } } }
StackOverflowError is a runtime error which points to serious problems that cannot be caught by an application. The java. lang. StackOverflowError indicates that the application stack is exhausted and is usually caused by deep or infinite recursion.
Creating an instance of Form1 would cause the f1 property to be initialised with an instance of Form1 which would cause the f1 property to be initialised with an instance of Form1 which would cause the f1 property to be initialised with an instance of Form1 which would cause the f1 property to be initialised with an instance of Form1 which would cause the f1 property to be initialised with an instance of Form1 which would cause the f1 property to be initialised with an instance of Form1 which would cause the f1 property to be initialised with an instance of Form1 which would cause the f1 property to be initialised with an instance of Form1 which would cause the f1 property to be initialised with an instance of Form1 which would cause the f1 property to be initialised with an instance of Form1 which would cause the f1 property to be initialised with an instance of Form1 which would cause the f1 property to be initialised with an instance of Form1 which would cause the f1 property to be initialised with an instance of Form1 which would ... Stack Overflow!
Within a method in the class, the 'f1' would be local and only exist for the life of the call. Unless you called the same method on the instantiated Form1 then there wouldn't be subsequent Form1s created.
You're creating an private instance of Form1 when Form1 is created so this is sort of an endless loop:
Somewhere in your code you create your first Form1 instance. When this instance is creating 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 like this:
Form1 f1 = new Form1()
this automaticly instatiates a new instance of the form.
I suggest you don't have a new instance of your Form1 inside your Form1 but if you really need this create a method to make the instance:
Change the Form1 f1 = new Form1();
into Form1 f1;
.
And create a method:
public void InstantiateNewForm1Instance()
{
f1 = new Form1();
}
But: DON'T CALL THIS METHOD IN THE CONSTRUCTOR! Or else you will have the same problem :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With