Jerry Murphy
Jerry Murphy

Reputation: 348

Error when instantiating a form with arguments

This loads the correct form

private void loadResults()
    {
        Results userResultsForm = new Results();
        userResultsForm.Show();
        this.Hide();
    }

However this loads a blank form

private void loadResults()
    {
        Results userResultsForm = new Results(correctAnswers);
        userResultsForm.Show();
        this.Hide();
    }

This the code in Results

public Results()
    {
        InitializeComponent();
    }

    public Results(bool[] correctAnswers)
    {
        // TODO: Complete member initialization
        this.correctAnswers = correctAnswers;
    }

InitializeComponent is called first

Upvotes: 0

Views: 19

Answers (1)

Luaan
Luaan

Reputation: 63732

It sounds like your Results constructor is wrong, and doesn't call the InitializeComponent method.

The usual pattern for alternate constructors for a form would be something like this:

public Results()
{
  InitializeComponent();
}

public Result(Answer[] answers) : this()
{
  // Do whatever you need with the answers
}

This makes sure that the "basic" constructor runs before yours, initializing the form properly.

Upvotes: 2

Related Questions