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

不想你离开。 提交于 2019-12-28 03:11:06

问题


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();
}

回答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();
}



回答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.



来源:https://stackoverflow.com/questions/860370/an-object-reference-is-required-for-the-non-static-field-method-or-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!