一个对象引用是所必需的非静态字段,方法或特性? [英] An object reference is required for the non-static field, method, or property?
问题描述
我知道这可能是一个非常newbish的问题,所以我很抱歉。
我试图从另一种形式访问Form1上放置一个标签的Text属性,MaxScore。
当我点击MaxScore Ok按钮,我想用max.ToString()来设置Form1的myGameCountLbl.Text到Form1的变量,最大
下面是我的code在MaxScore的OK按钮事件:
私人无效okBtn_Click(对象发件人,EventArgs的发送)
{
Form1.myGameCountLbl.Text = Form1.max.ToString();
Form1.compGameCountLbl.Text = Form1.max.ToString();
}
但是,当我去编译它,我得到的错误:
需要一个对象引用非静态字段,方法或属性Towergame_2.Form1.myGameCountLbl
块引用>我得到Towergame_2.Form1.max和Towergame_2.Form1.compGameCountLbl同样的错误。
不太清楚如何解决这一问题。马克斯是一个公共变量和两个标签是耻骨为好。
谢谢!
这是我在构造函数中的code(谢谢你lassevk为code!):
公共Form1中()
{
//初始化瓦尔
myHp = 100;
compHp = 100;
youWon = 0;
compWon = 0;
钱= 100;
canCompAttack = TRUE;
GAMEOVER = FALSE; //显示HowToPlay对话
HowToPlay howToPlay =新HowToPlay();
howToPlay.ShowDialog(); 使用(MaxScore maxScore =新MaxScore())
{
maxScore.MainForm =这一点;
maxScore.ShowDialog();
} 的InitializeComponent();
}
解决方案是任何机会Form1上类的名称?
您需要有表单类的一个实例的引用。
由于
okBtn
是不一样的形式,你需要给MaxScore
形成一个参考的Form1中
实例。例如,你可以将它添加到你的
MaxScore
的形式:公共Form1中的MainForm {搞定;组; }
然后在你的
okBtn_Click
方法,你会这样写:私人无效okBtn_Click(对象发件人,EventArgs的发送)
{
MainForm.myGameCountLbl.Text = MainForm.max.ToString();
MainForm.compGameCountLbl.Text = MainForm.max.ToString();
}然后当你构建
MaxScore
从Form1中
(我假设这是你在做什么):使用(MaxScore scoreForm =新MaxScore())
{
scoreForm.MainForm =这一点;
scoreForm.ShowDialog();
}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(); }
解决方案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 theMaxScore
form a reference to theForm1
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
fromForm1
(I'm assuming that's what you're doing):using (MaxScore scoreForm = new MaxScore()) { scoreForm.MainForm = this; scoreForm.ShowDialog(); }
这篇关于一个对象引用是所必需的非静态字段,方法或特性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!