• Register
0 votes
973 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