C# 非静态字段、方法或属性是否需要对象引用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/860370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An object reference is required for the non-static field, method, or property?
提问by Eric
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.
我正在尝试从另一个表单 MaxScore 访问 Form1 上标签的 Text 属性。
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().
当我单击 MaxScore 上的 Ok 按钮时,我想使用 max.ToString() 将 Form1 的 myGameCountLbl.Text 设置为 Form1 的变量 max。
Here is my code in the OK button event of MaxScore:
这是我在 MaxScore 的 OK 按钮事件中的代码:
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'
非静态字段、方法或属性“Towergame_2.Form1.myGameCountLbl”需要对象引用
I get the same error for Towergame_2.Form1.max and Towergame_2.Form1.compGameCountLbl.
Towergame_2.Form1.max 和 Towergame_2.Form1.compGameCountLbl 出现同样的错误。
Not quite sure how to fix this. Max is a public variable and the two labels are pubic as well.
不太确定如何解决这个问题。Max 是一个公共变量,两个标签也是公共变量。
Thanks!
谢谢!
This is the code in my constructor (thank you lassevk for the code!):
这是我的构造函数中的代码(感谢 lassevk 的代码!):
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();
}
采纳答案by Lasse V. Karlsen
Is by any chance Form1 the name of the class?
Form1 是班级的名称吗?
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.
由于okBtn
不在同一个表单上,您需要为MaxScore
表单提供对Form1
实例的引用。
For instance, you can add this to your MaxScore
form:
例如,您可以将其添加到MaxScore
表单中:
public Form1 MainForm { get; set; }
And then in your okBtn_Click
method, you'll write this:
然后在你的okBtn_Click
方法中,你会这样写:
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):
然后当你MaxScore
从Form1
(我假设这就是你正在做的)构建时:
using (MaxScore scoreForm = new MaxScore())
{
scoreForm.MainForm = this;
scoreForm.ShowDialog();
}
回答by nkirkes
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.
我同意@lassevk 关于解决您的问题的看法。我还建议将设置标签的行为包装到 Form1 类中的一个方法中,这只是帮助保持您的代码更清晰,并保持对要更新的字段以及如何更新它们包含在父表单中的责任/知识。您只需在 Form1 中定义一个公共方法,该方法接受一个字符串值并使用该值更新特定标签。然后在 MaxScore 表单中,在按钮单击事件处理程序中,您将调用该方法而不是尝试直接访问这些标签控件。
Just food for thought.
只是深思熟虑。