An object reference is required for the nonstatic field, method, or property

孤者浪人 提交于 2019-12-13 13:48:18

问题


I know people have asked about this question before but the scenario's were too specific and I am confused about the fundamentals.

I have two basic versions of a C# program, one that works, and one that doesn't. I would love it if someone could please explain why I get the error An object reference is required for the nonstatic field, method, or property in the second program.

Works:

namespace Experiments
{
    class Test
    {
        public string myTest = "Gobbledigook";

        public void Print()
        {
            Console.Write(myTest);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test newTest = new Test();
            newTest.Print();
            while (true)
                ;
        }
    }
}

Does not work:

namespace Experiments
{
    class Test
    {
        public string myTest = "Gobbledigook";

        public void Print()
        {
            Console.Write(myTest);
        }
    }

    class Program
    {
        public Test newTest = new Test();

        static void Main(string[] args)
        {
            newTest.Print();
            while (true)
                ;
        }
    }
}

When I try to Print() the text from the Test() class in the second program, it gives me that error An object reference is required for the nonstatic field, method, or property, and I don't understand why. I can see it has to do with where I declare an instance of the Test() class, but I don't remember anything like this happening in C++, so it mystifies me.

What's going on?


回答1:


It is not because of the definition of the class, it is all about the usage of keyword static.

The newTest object of the class Test is a public member of the class Program and the main is a static function inside the program class. and it is clearly mentioned in the error message An object reference is required for the non-static method. So what you need is Declare the newTest object as static in order to access them in static methods like main.

like this

 public static Test newTest = new Test();

An Additional Note

Consider that you ware defining the method Print as static inside the class Test as like the following:

 public static void Print()
 {
    Console.Write(myTest);
 }

Then you cant call the method like what you are currently using(newTest.Print();). Instead of that you have to use Test.Print();, Since A static member cannot be referenced through an instance. Instead, it is referenced through the type name. For example, consider the following class




回答2:


In the first program you have created a new instance inside a static method. Inside this method it's OK to do anything.

But when you want to call some methods or access some variables outside of static methods, you need them to be static. The reason is when you call a static method, no instance of a class is made and consequently no instance of non-static variables are made yet and you have no access to them!

So, in the second program the newTest variable initiation line is not executed until you have some line of code outside of Program class like Program p = new Program();. The solution is you make the variable static to be able to access it outside of the static Print() method, Or you can convert your Min() method to non-static mode which is impossible for Main() method exceptionally.

If you want to define a global variable, then I suggest you to define a special class e.x. MyGlobals:

public class SomeClass
{
    public int x;
}

public class MyGlobals
{
    public static SomeClass mySharedVariable = new SomeClass();

    public SomeClass myGlobalVariable = null;
}

// Usage:
class Program
{
    static void Main(string[] args)
    {
        MyGlobals.mySharedVariable.x = 10; // Shared among all instances
        MyGlobals myGlobal = new MyGlobals(); // New instance of MyGlobals
        myGlobal.myGlobalVariable = new SomeClass(); // New instance of SomeClass
        myGlobal.myGlobalVariable.x = 10; // One instance of MyGlobals including one instance of SomeClass
    }
}


来源:https://stackoverflow.com/questions/40145070/an-object-reference-is-required-for-the-nonstatic-field-method-or-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!