Cannot access a non-static member of outer type via nested type

后端 未结 4 1708
渐次进展
渐次进展 2020-11-30 12:53

I have error

Cannot access a non-static member of outer type \'Project.Neuro\' via nested type \'Project.Neuro.Net\'

with cod

4条回答
  •  执笔经年
    2020-11-30 13:42

    Even though class is nested within another class, it is still not obvious which instance of outer class talks to which instance of inner class. I could create an instance of inner class and pass it to the another instance of outer class. Therefore, you need specific instance to call this OtherMethod().

    You can pass the instance on creation:

    class Neuro
    {
        public class Net
        {
            private Neuro _parent;
            public Net(Neuro parent)
            {
             _parent = parent;
            }
            public void SomeMethod()
            {
                _parent.OtherMethod(); 
            }
        }
    
        public int OtherMethod() 
        {
            return 123;  
        }
    }
    

提交回复
热议问题