CS0120: An object reference is required for the nonstatic field, method, or property 'foo'

unity an object reference is required for the non-static field, method, or property
an object reference is required for the non static field, method, or property dbcontext database
an object reference is required for the non-static field automapper
an object reference is required for the non static field, method, or property 'object gettype
an object reference is required for the non-static field method or property label
an object reference is required for the non static field _context
an object reference is required for the non static field, method, or property page request
an object reference is required for the non-static field dependency injection

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 == "")
            {
                MessageBox.Show("Input any no");
            }
            else
            {
                val = Convert.ToInt32(textBox1.Text);
                Thread ot1 = new Thread(new ParameterizedThreadStart(SumData));
                ot1.Start(val);
            }
        }

        private static void ReadData(object state)
        {
            System.Windows.Forms.Application.Run();
        }

        void setTextboxText(int result)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new IntDelegate(SetTextboxTextSafe), new object[] { result });
            }
            else
            {
                SetTextboxTextSafe(result);
            }
        }

        void SetTextboxTextSafe(int result)
        {
            label1.Text = result.ToString();
        }

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

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

        delegate void IntDelegate(int result);

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

Why is this error occurring?

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


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.
    }
    
  2. 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.

  3. 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);
    }
    

More info about this error can be found on MSDN.

Compiler Error CS0120, An object reference is required for the nonstatic field, method, or property '​member'. In order to use a non-static field, method, or property, you  Another way would be to define an extension method for Test1 using a static class. Let me know if you need more of an example and I'll make an edit. See also CS0120: An object reference is required for the nonstatic field, method, or property 'foo'


You start a thread which runs the static method SumData. However, SumData calls SetTextboxText which isn't static. Thus you need an instance of your form to call SetTextboxText.

error An object reference is required for the non-static field, method , An alternative to requiring that you create an instance of Program is that you make pt() itself a static method. Since pt() only operates on its  An object reference is required for the non-static field, method, or property 'VM_Placement.Program.GetRandomBits()' CS0120: An object reference is required for


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

Go to your Program.cs and change

Application.Run(new Form1());

to

public static Form1 form1 = new Form1(); // Place this var out of the constructor
Application.Run(form1);

Now you can access a control with

Program.form1.<Your control>

Also: Don't forget to set your Control-Access-Level to Public.

And yes I know, this answer does not fit to the question caller, but it fits to googlers who have this specific issue with controls.

[Solved] error An object reference is required for the non-static field , How do you solve an object reference is required for the nonstatic field? An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.TextboxText().. same with labels 1 what does "an object reference is required for the non-static field, method, or property 'Random.Next(int, int)' mean?


Your method must be static

static void setTextboxText(int result)
{
    if (this.InvokeRequired)
    {
        this.Invoke(new IntDelegate(SetTextboxTextSafe), new object[] { result }); 
    }
    else
    {
        SetTextboxTextSafe(result);
    }
}

Object References in C#, However I am getting the error: An object reference is required for the non-static field, method, or property 'samples.PackageMetadata. So to try to explain, if you have several DoorScript in your scene, how does it know which instance of the script to call? Even if you only have 1 DoorScript in your scene, it doesn't know that it should call it on that one. You need to make it non-static and reference the DoorScript that is in your scene or whichever one you want to target.


Credit to @COOLGAMETUBE for tipping me off to what ended up working for me. His idea was good but I had a problem when Application.SetCompatibleTextRenderingDefault was called after the form was already created. So with a little change, this is working for me:

static class Program
{
    public static Form1 form1; // = new Form1(); // Place this var out of the constructor

/// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(form1 = new Form1()); } }

When to use static methods C#, Project File Line Suppression State Error CS0120 An object reference is required for the non-static field, method, or property 'Car.color' ConsoleApp3  C#: An object reference is required for the non-static field, method, or Property; an object reference is required for non static field method or property MyApplication.MyApplicationBl.TeamService.GetTeams() An object reference is required non-static field, method, or property error; An object reference is required for the nonstatic field, method, or property 'member' CS0120: An object reference is required; Getting Object reference required for the non-static method


Static Method In C#, I'm getting the error "An object reference is required for the non-static field, method, or property 'PlayerStats.GainCurrency(float)' The code  An object reference is required for the non-static field, method, or property 'Controls_fccDiaryDayNew.UserControlButtonClicked' U cannot refer a non-static thing directly in a static method, plz make that static.


C# error An object reference is required for the non-static, An object reference is required for non-static field, method, or property. I started experimenting with Unity only today. I was going through a  An object reference is required for the non-static field, method, or property () Do you know what an Object reference is? Take a look at:


C# : Error CS0120 An object reference is required for the non-static , Hello, I am getting the following error in my Unity project: An object reference is required for the non-static field, method, or property. I am trying to use one script