7

I have error

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

with code like this (simplified):

class Neuro
{
    public class Net
    {
        public void SomeMethod()
        {
            int x = OtherMethod(); // error is here
        }
    }

    public int OtherMethod() // its outside Neuro.Net class
    {
        return 123;  
    }
}

I can move problematic method to Neuro.Net class, but I need this method outside.

Im kind of objective programming newbie.

Thanks in advance.

2
  • 1
    Seems pretty obvious - OtherMethod is a member of another type. The fact that it's nested does not mean the methods are inherited. Commented May 1, 2013 at 15:19
  • 1
    See this SO answer for more info: stackoverflow.com/a/5393472/1451531 Commented May 1, 2013 at 15:21

5 Answers 5

21

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;  
        }
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

I made my method static. Now im wondering how shall I reorganize my classes... maybe its good subject for another question.
2

you need to instantiate an object of type Neuro somewhere in your code and call OtherMethod on it, since OtherMethod is not a static method. Whether you create this object inside of SomeMethod, or pass it as an argument to it is up to you. Something like:

// somewhere in the code
var neuroObject = new Neuro();

// inside SomeMethod()
int x = neuroObject.OtherMethod();

alternatively, you can make OtherMethod static, which will allow you to call it from SomeMethod as you currently are.

1 Comment

@Kamil don't worry about it, D Stanley's answer is more complete anyway.
0

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;  
    }
}

Comments

0

I think making an instance of outer class in inner class is not a good option because you may executing business logic on outer class constructor. Making static methods or properties is better option. If you insist making an instance of outer class than you should add another parameter to outer class contructor that not to execute business logic.

Comments

-1

you acces a methode of a class from other class you can not do this.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.