0

I am trying to create a menu for a game I' m developing. This is the source code:

   using System;
   using System.Collections.Generic;
   using System.ComponentModel;
   using System.Data;
   using System.Drawing;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;
   using System.Windows.Forms;

namespace Racing_Manager
{

public partial class Form1 : Form
{
    Form1 form1 = new Form1();
    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    //Exit Button
    private void button1_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
    //Help Button
    private void Help_Click(object sender, EventArgs e)
    {

    }

    //Play Button
    private void Play_Click(object sender, EventArgs e)
    {
        Hide();
        Form2 secondMenu = new Form2();
        secondMenu.Show();

    }
    }
    }

Then I have:

namespace Racing_Manager
{
public partial class Form2 : Form
{
    public Form2 form2 = new Form2();
    public Form2()
    {
        InitializeComponent();
    }

    //Back Button
    private void button2_Click(object sender, EventArgs e)
    {
        Hide();
        Form1 form1 = new Form1();
        form1.Show();

    }
    }
    }

When I run this, it gives a System.StackOverflowException (I know, quite appropriate) on this line:

    Form1 form1 = new Form1();

What am I doing wrong? How do I fix it? Anything I can do to improve the code quality?

4 Answers 4

5

Your problem is in the first lines:

public partial class Form1 : Form
{
    Form1 form1 = new Form1();

What should this line do?

When you create an instance of Form1 it's members are initialized. Here you declared a member form1 that is initialized by creating a new instance of Form1.

When you create an instance of Form1 it's members are initialized. Here you declared a member form1 that is initialized by creating a new instance of Form1.

When you create an instance of Form1 it's members are initialized. Here you declared a member form1 that is initialized by creating a new instance of Form1.

When you create an instance of Form1 it's members are initialized. Here you declared a member form1 that is initialized by creating a new instance of Form1.

When you create an instance of Form1 it's members are initialized. Here you declared a member form1 that is initialized by CREATING A NEW INSTANCE OF Form1.

StackOverflowException

because you recursivly call the constructor of Form1 and so your call stack eventually runs full.

The same goes for Form2.


Conclusion: remove this line from your Form1 class (and the one from Form2), you don't need it anyway.

Sign up to request clarification or add additional context in comments.

3 Comments

the same is applicable to Form2!
@klappvisor yep, forgot to add that, did it now.
Warning: this answer may throw a StackOverflowException if the OP continues to initialize instances of Form1.
2

It's the very first line that's wrong:

  public partial class Form1 : Form {
    Form1 form1 = new Form1(); // <- this one

You're creating Form1 instance, but Form1 should initialize Form1 form1 field, which in turn creates Form1 instance with its own Form1 form1 to be initialized...

1 Comment

That, to me, sounds like the second line.
1

You have a property Form1 inside your Form1 class. It cause an infinite loop.

So you should change :

public partial class Form1 : Form
{
    Form1 form1 = new Form1();
    public Form1()
    {
        InitializeComponent();

    }...

to

public partial class Form1 : Form
{  
    public Form1()
    {
        InitializeComponent();

    }...

Comments

0

As the other answer state, Form1 and Form2 both create an instance of themselves when a new instance is constructed. It looks like you want a singleton. To achieve this change your code like so

//From
public Form1 = new Form1()
//To
public static Form1 = new Form1();

This will create an instance of the Form as soon as the Form1 class is referenced for the first time. So Form1.Form1 would automatically create a single instance.

2 Comments

Thanks, it solved the problem. However, when I am switching between forms, the first one closes, you think the app crashed, but then the other form appears. How do I solve this?
Accept my answer, then post your code in a new question and post a link in these comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.