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


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 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;              (int = icount; > 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();         }     } } 

could gave me answer why error occurring?

an object reference required nonstatic field, method, or property 'windowsapplication1.form1.settextboxtext(int)

it looks calling non static property static method. need either make property static, or create instance of form1.

static void settextboxtextsafe(int result) {     label1.text = result.tostring(); } 

or

private static void sumdata(object state) {     int result;     //int[] icount = (int[])state;     int icount = (int)state;      (int = icount; > 0; i--)     {         result += i;         system.threading.thread.sleep(1000);     }     form1 frm1 = new form1();     frm1.settextboxtext(result); } 

Comments