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

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.

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 2
3,890 points

Related questions

0 votes
1 answer 10 views
0 votes
1 answer 9 views
9 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
0 votes
1 answer 39 views
39 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, 2020 Raphael Pacheco 4.9k points
1 vote
2 answers 123 views
123 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, 2020 Martin K 6.6k points
0 votes
1 answer 21 views
0 votes
1 answer 22 views
22 views
Problem: cannot make a static reference to the non-static method getclass() from the type object
asked Feb 24 Hira 2.2k points
0 votes
1 answer 15 views
15 views
Does anyone have any ideas? Any help is appreciated.
asked Feb 11 TeamScript 13.5k points
0 votes
1 answer 44 views
44 views
Problem: I have recently joined a new project in WPF C# and at this time I am trying to animate the image to simply move down. I have already found out the "MoveTo" function on an Internet but when I pasted it in my code then the below error occurred: &ldquo; ... button_Click(object sender, RoutedEventArgs e)     {         MoveTo(image, 130);     } } Kindly help me in fixing my above C# code.
asked Jul 28, 2020 Raphael Pacheco 4.9k points
0 votes
2 answers 761 views
761 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, 2020 jwilliam 3.9k points
0 votes
1 answer 36 views
36 views
I want to make a static reference to the non-static method.
asked Sep 22, 2020 Daniel Anderson 4k points