Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
}
like image 609
Eric Avatar asked May 13 '09 20:05

Eric


People also ask

How do you fix an object reference is required for the non static field method or property in C#?

To correct this error, either (i) declare a variable of type program and then call the method or (2) add 'static' to the declaration of method pt().

What is object reference for non static field?

If a method is non-static, you need an instance (also called an "object reference") of the class to call it. If it is static, you just need the class name to call it. If we add some code to your Main method, it might become a little clearer. The Go method itself does not have an instance, so it cannot call Hello.

How do I fix NullReferenceException object reference not set to an instance of an object?

The best way to avoid the "NullReferenceException: Object reference not set to an instance of an object” error is to check the values of all variables while coding. You can also use a simple if-else statement to check for null values, such as if (numbers!= null) to avoid this exception.

How do I call a non static method from a static method in C#?

But when we try to call Non static function i.e, TestMethod() inside static function it gives an error - “An object refernce is required for non-static field, member or Property 'Program. TestMethod()”. So we need to create an instance of the class to call the non-static method.


1 Answers

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();
}
like image 85
Lasse V. Karlsen Avatar answered Oct 07 '22 16:10

Lasse V. Karlsen