Reputation: 706
So far i have this code that i want to use to be able to call the same function in the child form.
Parent Form Code:
FormGame frmGame; // i put this here so that in the second function it doesnt complain about frmGame not being set.
public void CreateGame(string Level) // this function is called first
{
FormGame frmGame = new FormGame(this); // i need both functions to be able to access this instance of the child form
frmGame.SetLevel(Level); // sets the text of a label in the child form
frmGame.Show();
}
public void UpdateGame(string Level) // then this function is called second
{
frmGame.SetLevel(Level); // to update the same label as set in the first method
}
the problem with this code is that while yes it doesn't come up with any errors when just sitting there but when debugging it when the second function is called it cant find the instance of FormGame that was set in the first function so frmGame is null.
I have tried:
Upvotes: 0
Views: 131
Reputation: 66449
Your code creates a new instance of FormGame
, whose scope is only inside that function. In no way does this affect the frmGame
variable you defined outside of the method.
FormGame frmGame = new FormGame(this);
To avoid getting an error when you call UpdateGame
, don't define a new variable inside the method.
public void CreateGame(string Level)
{
frmGame = new FormGame(this); // use the class-level field
...
Upvotes: 4