• Register
0 votes
1.2k views

Problem :

Why is the below error occurring?

An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.setTextboxText(int)

 

3.9k points

Please log in or register to answer this question.

3 Answers

0 votes

Solution :

It looks like you are calling the non static property from the static method. You will need to either make a property static, or create the instance of your Form1 as shown below :

static void SetTextboxTextSafe(intmy myresult)
{
    label1.Text = myresult.ToString();
}

OR

private static void SumData(object mystate)
{
    int myresult;
    //int[] icount = (int[])state;
    int myicount = (int)mystate;

    for (int j = jcount;j > 0; j--)
    {
        myresult += j;
        System.Threading.Thread.Sleep(1000);
    }
    Form1 myfrm1 = new Form1();
    myfrm1.setTextboxText(myresult);
}

 

OR

For your case, where you want to get the Control of the Form and are receiving above error, then I have the little bypass for you.

Go to your Program.cs and change below line

Application.Run(new Form1());

To the below code

public static Form1 myform1 = new Form1(); // Place a var out of a constructor
Application.Run(myform1);

Now you can access the control with below code

Program.myform1.<Your control>

Please don't forget to set the Control-Access-Level to a Public.

 

OR

You start the thread which runs a static method SumData. But, SumData calls  SetTextboxText which is not a static. So you need the instance of your form to call a  SetTextboxText.

38.6k points
0 votes

Solution:

Since Main is static, you cannot use the rest of the Program Class inside your static method.
Therefore you need an instance of the Program Class.

Change

int j;        
j = pt(3, 4);

Into

int j;
Program P = new Program();
j = P.pt(3, 4);

you are calling an instance method pt() from a static method (Main). 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().

// option 1
int j;
Program program = new Program(); // new line
j = program.pt(3, 4);            // replace this line "j = pt(3, 4);"

// option 2
public static int pt(int l, int r)
{
    return l + r;
}

It looks like you are calling a non static member (a property or method, specifically setTextboxText) from a static method (specifically SumData). You will need to either:

  1. Make the called member static also

static void setTextboxText(int result)
{
    // Write static logic for setTextboxText.  
    // This may require a static singleton instance of Form1.
}
  1. Create an instance of Form1 within the calling method:

private static void SumData(object state)
{
    int result = 0;
    //int[] icount = (int[])state;
    int icount = (int)state;

    for (int i = icount; i > 0; i--)
    {
        result += i;
        System.Threading.Thread.Sleep(1000);
    }
    Form1 frm1 = new Form1();
    frm1.setTextboxText(result);
}

Passing in an instance of Form1 would be an option also

  1. Make the calling method a non-static instance method (of Form1):

private void SumData(object state)
{
    int result = 0;
    //int[] icount = (int[])state;
    int icount = (int)state;

    for (int i = icount; i > 0; i--)
    {
        result += i;
        System.Threading.Thread.Sleep(1000);
    }
    setTextboxText(result);
}

 

 

31.7k points
0 votes

This error usually occurs when you don’t create object instance for methods, properties or fields

Solution:

In order to use a non-static field, method, or property, you must have to create an object instance. You will need to either;

  • Make the called member static also:
  • Create an instance of Form1 within the calling method:
  • Passing an instance of Form1 is optional.

Example code:

The following example will help to solve your problem.

Make the called member static also:

static void setTextboxText(int result)

{

    // Write static logic for setTextboxText. 

    // This may require a static singleton instance of Form1.

}

Create an instance of Form1 within the calling method:

private static void SumData(object state)

{

    int result = 0;

    //int[] icount = (int[])state;

    int icount = (int)state;



    for (int i = icount; i > 0; i--)

    {

        result += i;

        System.Threading.Thread.Sleep(1000);

    }

    Form1 frm1 = new Form1();

    frm1.setTextboxText(result);

}

Passing an instance of Form1 is optional.

Make the calling method a non-static instance method of Form1:

private void SumData(object state)

{

    int result = 0;

    //int[] icount = (int[])state;

    int icount = (int)state;



    for (int i = icount; i > 0; i--)

    {

        result += i;

        System.Threading.Thread.Sleep(1000);

    }

    setTextboxText(result);

}

I hope this will help to solve your problem.

3.9k points

Related questions

0 votes
1 answer 18 views
18 views
Problem : Is there a method to resolve this issue: C# an object reference is required for the non-static field, method, or property?
asked Jun 14 Sifat55 90.2k points
0 votes
1 answer 11 views
11 views
Problem: Consider: namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //int[] val = { 0, 0}; int val; if (textBox1.Text == "" ... } Why is this error occurring? An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.setTextboxText(int)
asked Apr 24 munim01 21k points
0 votes
1 answer 5 views
5 views
Problem: Where can I get assistance: Unity an object reference is required for the non-static field?
asked Jun 22 Shima 106k points
0 votes
1 answer 23 views
0 votes
1 answer 5 views
5 views
Problem: Where can I find an object reference which is required to access non-static field .
asked Jun 23 Diederich Raf 103k points
0 votes
1 answer 17 views
17 views
Problem: Working on this but no luck, need help > C# an object reference is required to access non-static field
asked May 8 Aurelia Maja 65.6k points
0 votes
1 answer 25 views
25 views
Problem: Need help with this > Object reference is required for the non-static field.
asked May 16 Priscilla Gurpreet 62.6k points
0 votes
1 answer 11 views
11 views
Problem: Where should I look for help with this issue? >Object reference is required to access non-static field.
asked May 11 Rada Gracelynn 91.8k points
0 votes
1 answer 15 views
15 views
Problem: Can anyone assist me with this problem? >An object reference is required to access non-static method.
asked May 8 Ashok Dileep 21.5k points
0 votes
1 answer 19 views
19 views
Problem: i get error "An object reference is required for the non-static field, method, or property 'text1.Program.method(int, int)'" error An object reference is required for the non-static field, method, or property
asked Dec 3, 2020 Mashhoodch 13k points