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

The problem is that nested classes are not derived classes, so the methods in the outer class are not inherited.

Some options are

  1. Make the method static:

    class Neuro
    {
        public class Net
        {
            public void SomeMethod()
            {
                int x = Neuro.OtherMethod(); 
            }
        }
    
        public static int OtherMethod() 
        {
            return 123;  
        }
    }
    
  2. Use inheritance instead of nesting classes:

    public class Neuro  // Neuro has to be public in order to have a public class inherit from it.
    {
        public static int OtherMethod() 
        {
            return 123;  
        }
    }
    
    public class Net : Neuro
    {
        public void SomeMethod()
        {
            int x = OtherMethod(); 
        }
    }
    
  3. Create an instance of Neuro:

    class Neuro
    {
        public class Net
        {
            public void SomeMethod()
            {
                Neuro n = new Neuro();
                int x = n.OtherMethod(); 
            }
        }
    
        public int OtherMethod() 
        {
            return 123;  
        }
    }
    

Leave a Comment