An object reference is required for the non-static field, method, or property?

23,505

Solution 1

Is by any chance Form1 the name of the class?

You need to have a reference to an instance of the form class.

Since okBtn is not on the same form, you need to give the MaxScore form a reference to the Form1 instance.

For instance, you can add this to your MaxScore form:

public Form1 MainForm { get; set; }

And then in your okBtn_Click method, you'll write this:

private void okBtn_Click(object sender, EventArgs e)
{
    MainForm.myGameCountLbl.Text = MainForm.max.ToString();
    MainForm.compGameCountLbl.Text = MainForm.max.ToString();
}

and then when you're constructing MaxScore from Form1 (I'm assuming that's what you're doing):

using (MaxScore scoreForm = new MaxScore())
{
    scoreForm.MainForm = this;
    scoreForm.ShowDialog();
}

Solution 2

I agree with @lassevk with regards to resolving your issue. I'd also recommend wrapping the behavior of setting the labels into a method within the Form1 class, which simply helps keep your code cleaner and keeps the responsibility/knowledge of what fields to update and how to update them contained within the parent form. You'd simply define a public method in Form1 that takes a string value and updates the specific labels with that value. Then in the MaxScore form, in your button click event handler, you'd call that method rather than try to access those label controls directly.

Just food for thought.

Share:
23,505
Eric
Author by

Eric

Updated on July 09, 2022

Comments

  • Eric
    Eric almost 2 years

    I know this is probably a very newbish question, so I apologize.

    I am trying to access the Text property of a label on Form1 from another form, MaxScore.

    When I click the Ok button on MaxScore, I want to set Form1's myGameCountLbl.Text to Form1's variable, max by using max.ToString().

    Here is my code in the OK button event of MaxScore:

    private void okBtn_Click(object sender, EventArgs e)
    {
        Form1.myGameCountLbl.Text = Form1.max.ToString();
        Form1.compGameCountLbl.Text = Form1.max.ToString();
    }
    

    But when I go to compile it, I get the error:

    An object reference is required for the non-static field, method, or property 'Towergame_2.Form1.myGameCountLbl'

    I get the same error for Towergame_2.Form1.max and Towergame_2.Form1.compGameCountLbl.

    Not quite sure how to fix this. Max is a public variable and the two labels are pubic as well.

    Thanks!

    This is the code in my constructor (thank you lassevk for the code!):

    public Form1()
    {
        //initialize vars
        myHp = 100;
        compHp = 100;
        youWon = 0;
        compWon = 0;
        money = 100;
        canCompAttack = true;
        gameOver = false;
    
        //show HowToPlay Dialogue
        HowToPlay howToPlay = new HowToPlay();
        howToPlay.ShowDialog();
    
        using (MaxScore maxScore = new MaxScore())
        {
            maxScore.MainForm = this;
            maxScore.ShowDialog();
        }
    
        InitializeComponent();
    }