Member '' cannot be accessed with an instance reference

前端 未结 10 1931
我在风中等你
我在风中等你 2020-11-22 10:39

I am getting into C# and I am having this issue:

namespace MyDataLayer
{
    namespace Section1
    {
        public class MyClass
        {
            publ         


        
10条回答
  •  鱼传尺愫
    2020-11-22 11:22

    This causes the error:

    MyClass aCoolObj = new MyClass();
    aCoolObj.MyCoolStaticMethod();
    

    This is the fix:

    MyClass.MyCoolStaticMethod();
    

    Explanation:

    You can't call a static method from an instance of an object. The whole point of static methods is to not be tied to instances of objects, but instead to persist through all instances of that object, and/or to be used without any instances of the object.

提交回复
热议问题