An object reference is required for the nonstatic field, method, or property 'member'
A variable declared outside of a method needs to have the same static declaration as the method in which it is used.
The following sample generates CS0120:
public class clx { public int i; // not static public static void Main() { } public static void f() { i = 9; // CS0120, in a static method // try the following lines instead // clx Myclx = new clx(); // Myclx.i = 9; } }
CS0120 will also be generated if there is a call to a nonstatic method from a static method:
using System; public class MyClass { public static void Main() { TestCall(); // CS0120 // To call a non-static method from Main, // first create an instance of the class // MyClass anInstanceofMyClass = new MyClass(); // anInstanceofMyClass.TestCall(); } public void TestCall() { } }
Similarly, a static method cannot call an instance method unless you explicitly give it an instance of the class. For example, the following sample also generates CS0120:
using System; public class x { public static void Main() { do_it("Hello There"); // CS0120 } private void do_it(string sText) { // you could also add the keyword static to the method definition // private static void do_it(string sText) { Console.WriteLine(sText); } }