-3

I'm making a simple game where you fly the ship around, dodging asteroids until they hit you and you blow up. I have a form for the game, and then when you get hit after 5 seconds (I'm using a timer) it will tick closing that form and opening a new form (EndGameForm). I'll show my code, but there seems to be a failure and my knowledge in C# is very limited.

private void GameEndTimer_Tick(object sender, EventArgs e)
{
        this.Visible = false;
        EndGameForm gform = new EndGameForm();
        gform.Show();
        GameEndTimer.Enabled = false;
        var frm2 = new EndGameForm(ScoreLabel.Text.ToString());
        frm2.Show();
}

That was in the first form, saving the text from the score label and opening the new form.

public EndGameForm(string s)
{
        InitializeComponent();
        Score1Label.Text = s;
}

That was in the second form, recalling the save and setting the new Highscore label.

Error Code: Error 1 'SemesterProject.EndGameForm' does not contain a constructor that takes 0 arguments \HHS-FS2\Home$\Students\10th Grade\985832\CP C#\SemesterProject\SemesterProject\GameForm.cs 82 33 SemesterProject

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 4
    The error is self-explanatory... You've passed a `string` as argument to constructor declaration of the `EndGameForm` class. But within its calling, no argument has been passed into... – User May 18 '15 at 14:10

1 Answers1

0

You need to pass something to constructor of the EndgameForm class, as it is supposed to be based on your design.

As a rule of thumb, that could be done by a string field class from the caller class:

private string _tobeSentAsArgument {get; set;}

after its initialization where ever you want, it is ready to be used:

private void GameEndTimer_Tick(object sender, EventArgs e)
{
    this.Visible = false;
    EndGameForm gform = new EndGameForm(this._tobeSentAsArgument);
    gform.Show();
    GameEndTimer.Enabled = false;
    var frm2 = new EndGameForm(ScoreLabel.Text.ToString());
    frm2.Show();
}
User
  • 952
  • 2
  • 21
  • 43