Reputation: 116
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?
Upvotes: 0
Views: 1049
Reputation: 23224
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.
Upvotes: 0
Reputation: 43876
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.
Upvotes: 5
Reputation: 6538
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();
}...
Upvotes: 1
Reputation: 186668
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...
Upvotes: 2