简体   繁体   English

无法通过嵌套类型访问外部类型的非静态成员

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

I have error 我有错误

Cannot access a non-static member of outer type 'Project.Neuro' via nested type 'Project.Neuro.Net' 无法通过嵌套类型“Project.Neuro.Net”访问外部类型“Project.Neuro”的非静态成员

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. 我可以将有问题的方法移到Neuro.Net类,但我需要在外面使用这个方法。

Im kind of objective programming newbie. 我是一种客观的编程新手。

Thanks in advance. 提前致谢。

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 : 使方法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 : 创建一个Neuro实例:

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

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. 你需要在代码中的某个地方实例化一个Neuro类型的对象,并在其上调用OtherMethod ,因为OtherMethod不是静态方法。 Whether you create this object inside of SomeMethod , or pass it as an argument to it is up to you. 是否在SomeMethod创建此对象,或将其作为参数传递给它取决于您。 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. 或者,您可以将OtherMethod静态,这样您就可以OtherMethod现在一样从SomeMethod调用它。

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() . 因此,您需要特定的实例来调用此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;  
    }
}

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. 如果您坚持创建外部类的实例,则应该向外部类构造器添加另一个不执行业务逻辑的参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法通过嵌套类型X访问外部类型X的非静态成员 - Cannot access a non-static member of outer type X via nested type X 无法通过嵌套类型“FormMain.ImageDelegateClass”访问外部类型“FormMain”的非静态成员 - Cannot access a non-static member of outer type 'FormMain' via nested type 'FormMain.ImageDelegateClass' 无法使用第三方提供的类通过嵌套类型访问外部类型XXX的非静态成员 - Cannot access a non-static member of outer type XXX via nested type with a third-party provided class C#无法访问外部类型的非静态成员 - C# cannot access a non-static member of outer type 无法访问外部类型的非静态成员 - Cannot access a non-static member of outer type 使标签静态错误:无法通过嵌套类型“windowsForm8.Form1.DBConnect”访问外部类型“windowsForm8.Form1”的非静态成员 - make a label static error : cannot access a non-static member of outer type 'windowsForm8.Form1' via nested Type 'windowsForm8.Form1.DBConnect' 无法通过嵌套类型访问外部类型的非静态成员 - Cannot access a nonstatic member of outer type… via nested type 嵌套类:无法访问静态上下文中的非静态字段 - Nested class: Cannot access non-static field in static context 无法访问外部类型的非静态成员 - Cannot access a nonstatic member of outer type 使用静态类型成员以确保非静态类中的类型安全 - Using static type member to ensure type-safety in a non-static class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM