• Register
0 votes
424 views

Problem :

Why is the below error occurring?

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

 

7 5 2
3,870 points

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.

9 7 4
38,600 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);
}

 

 

10 6 4
31,120 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.

11 5 1
3,890 points

Related questions

0 votes
1 answer 5 views
0 votes
1 answer 29 views
29 views
Problem: I am beginner to C#. Please find below my code for your help: public class MySoundManager : MonoBehaviour { public List<AudioSource> audioSounds = new List<AudioSource>(); public double minTime = 0.5; public static void playSound(AudioClip sourceSound, Vector3 objectPosition, int ... to create the object of a class or create a methods as the static one but I don&rsquo;t know how to do it.
asked Jul 22 Raphael Pacheco 4.9k points
1 vote
2 answers 61 views
61 views
Problem: I am beginner to C# with Unity engine. I have already started learning C# with Unity engine. I have tried to write the very basic script as shown below for your reference: using UnityEngine; using System.Collections; public class scriptMyBall : MonoBehaviour { / ... but unable to fix the Unity Engine related error. I need Unity Engine&rsquo;s expert who can guide me in fixing above error.
asked May 23 Martin K 6.6k points
0 votes
2 answers 400 views
400 views
Problem : I am unable to figure out what is wrong with my code. When I try to compile my code I get the below message: &ldquo;Program does not contain a static 'main' method suitable for an entry point&rdquo;
asked Jan 29 jwilliam 3.9k points
0 votes
1 answer 11 views
11 views
I want to make a static reference to the non-static method.
asked Sep 22 Daniel Anderson 4k points