48

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

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.

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.

Rob
  • 2,080
  • 4
  • 28
  • 48

6 Answers6

62

In C# there is actually no implicit reference to the instance of the enclosing class, so you need to pass such a reference, and a typical way of doing this is through the nested class' constructor.

Jpsy
  • 20,077
  • 7
  • 118
  • 115
Mircea Grelus
  • 2,905
  • 1
  • 20
  • 14
28

You can use something like this:


class B {
    private MainClass instance;

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

    List SubSetList;

    public void AddNewItem(A NewItem) {
       Check MasterListHere ????
    }
  }
Oleks
  • 31,955
  • 11
  • 77
  • 132
  • 5
    This answer needs more detail, especially showing the main class and describing why this is the best method of doing this (unless there are better ones). The answer at the moment requires too much interpretation. – TheForgot3n1 Mar 05 '22 at 17:39
5

With your definition, instances of class B may access the private methods and static fields of class MainClass.

Steve Guidi
  • 19,700
  • 9
  • 74
  • 90
1

Yes, but you would need to have a reference to the instance of MainClass from within you instance of B.

Have you thought of re-working your classes a little bit? Istead of having the AddNewItem method in B, you could have it in MainClass. That way it would be easy to check the MasterList from within MainClass.

Jeremy
  • 44,950
  • 68
  • 206
  • 332
1

This might work for you:

public void AddNewItem(A newItem, Func<A, bool> checkForItemInMasterList)
{
    if (checkForItemInMasterList.Invoke(newItem)
        SubList.Add(newItem);
}

You would then have something in your MainClass to use the method like this:

public void AddItem(A newItem)
{
    new B().AddNewItem(newItem, x => MasterList.Contains(x));
}

To summarize it might look something like this:

class MainClass {
  class A { Whatever }

  class B {
    List<A> SubSetList;

    public void AddNewItem(A newItem, Func<A, bool> checkForItemInMasterList)
    {
        if (checkForItemInMasterList.Invoke(newItem)
            SubList.Add(newItem);
    }
  }

  //I don't know how you're adding items to instances of B.
  //This is purely speculative.
  public void AddItem(A newItem)
  {
      new B().AddNewItem(newItem, x => MasterList.Contains(x));
  }

  List<A> MasterList;
}
Joseph
  • 25,330
  • 8
  • 76
  • 125
  • Problem is that i'll have multiple instances of B that i'll be adding A items to... – Rob Jul 09 '09 at 19:40
  • I'm considering doing something similar to this, but including a reference to B as well.. so public void AddItem(B list, A item) {}.. – Rob Jul 09 '09 at 19:42
  • @Rob multiple instances are fine in this case, you just have to change the way you add items in your MainClass, but that way you don't have to worry about B knowing about your MasterList. – Joseph Jul 09 '09 at 19:44
0

Regardless of whether you nest class B inside class A, any given instance of A will still need to know about the instance of B that it is held by. So you might want to initialize A with a reference to B and keep it in a field. This likely includes un-nesting it.

Steven Sudit
  • 19,391
  • 1
  • 51
  • 53
  • I considered that too... Is there an access modifier that will limit access only to nested classes? – Rob Jul 09 '09 at 19:33
  • Well, one way is to put all of these data structures into their own assembly and use internal instead of private. – Steven Sudit Jul 09 '09 at 19:35
  • i considered that, but it doesn't prevent other classes to access this list. It's not super critical, but I'd like this to be as clean as possible... – Rob Jul 09 '09 at 19:41
  • Then I think Alex's solution will do. – Steven Sudit Jul 10 '09 at 02:31