C#嵌套类访问父成员 [英] C# Nested Class Access Parent Member

查看:694
本文介绍了C#嵌套类访问父成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在子类...

Is it possible to access a parent member in a child class...

class MainClass {
  class A { Whatever }

  class B {
    List<A> SubSetList;

    public void AddNewItem(A NewItem) {
       Check MasterListHere ????
    }
  }

  List<A> MasterList;
}



所以...我的主类将有一个主列表。它也将有一大堆的B的实例的B每个实例的,我想增加新的A对特定的B,但只有当他们在主列表存在。我玩弄使得MasterList静态和它的作品...直到我有MainClass ...的多个实例,我都会有。

So... my main class will have a master list. It will also have a bunch of instances of B. In each instance of B, I want to add new A's to the particular B, but only if they exist in the Master List. I toyed with making the MasterList static and it works ... until I have more than one instance of MainClass... which I will have.

我可以通过一个参考到MasterList到b的每个实例,但我最终将这些MasterLists的多,我不希望有通过大量文献的,如果我没有。

I could pass a reference to MasterList to each instance of B, but I will eventually have multiple of these "MasterLists" and i don't want to have to pass lots of references if i don't have to.

推荐答案

您可以使用这样的:


class B {
    private MainClass instance;

    public B(MainClass instance)
    {
        this.instance = instance;
    }

    List SubSetList;

    public void AddNewItem(A NewItem) {
       Check MasterListHere ????
    }
  }

这篇关于C#嵌套类访问父成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆