0

Im having a problem trying to reference values across forms, i tried using the method below but im receiving an error.

Error: "System.NullReferenceException: 'Object reference not set to an instance of an object.' tmp was null."

Is there another way I can reference values across forms or am i doing something wrong. Any Help would be appreciated.Thanks.

public partial class PlayMenu : Form
{
    private void PlayMenu_KeyDown(object sender, KeyEventArgs e)
    {
        if (finishline = true)
        {
            getNewTimeElapsed();
            MainMenu tmp = (MainMenu)this.Owner;
            tmp.addLastScoreLevel1(TimeElapsed);
            this.Close();
        }
    }
    public float getNewTimeElapsed()
    {
        return TimeElapsed;
    }
}


public partial class MainMenu : Form
{        
    public void addLastScoreLevel1(float newScore)
    {
        HighScoresMenu tmp2 = (HighScoresMenu)this.Owner;
        PlayMenu tmp3 = (PlayMenu)this.Owner;

        if (newScore < tmp2.HighScores[4])
        {
            tmp2.HighScores[4] = newScore;
            tmp2.HighScoresName[4] = getPlayersName();
            Array.Sort(tmp2.HighScores, tmp2.HighScoresName);
        }
    }
}

1 Answers1

0

So you need to get the parent class of PlayMenu.

Instead of

MainMenu tmp = (MainMenu)this.Owner;

try using

MainMenu tmp = (MainMenu)this.Parent;
elgaspar
  • 458
  • 3
  • 11