tags:

views:

74

answers:

7

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;
            }
        }
    }
}

}
+4  A: 

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 :-)

Zenuka
+1  A: 

Hey,

The form is instantiated, then it processes the private fields, which instantiates the form1, which then instantiates the private fields (finding form1), which then instantiates that object and processes the private fields, continuing on.

So that is why that happens, whereas, in a method, a method only executes when called, no internal initialization.

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

Brian
Erm... insanity?
Lazarus
+2  A: 

That's because every time you create an instance of your class, it creates another instance of your class and so on...and so on...and so on...

In other words, you have a infinite recursion when you try to create an instance of your class.

You need to add some kind of control around whether or not a new instance of Form1 gets created at construction 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;
    }
}
Justin Niessner
+1  A: 

Could it be because Form1 is trying to instantiate another Form1 which in turn is trying to instantiate another Form1 etc?

Dan Iveson
A: 

If each Form1 class contains one new instance of a Form1 class, you are bound to have a stack overflow exception when trying to create one.

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

Daniel Daranas
+5  A: 

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.

Lazarus
+1  A: 

If you're in the new constructor of Form1, an instance of Form1 is already being created. So, before it's finished being created, you're creating another, then that one does the same. Before your program loads, too many form1s are going on the stack...overflowing the stack.

MCain