tags:

views:

2228

answers:

6

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.

A: 

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
I considered that too... Is there an access modifier that will limit access only to nested classes?
Rob
Well, one way is to put all of these data structures into their own assembly and use internal instead of private.
Steven Sudit
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
Then I think Alex's solution will do.
Steven Sudit
+2  A: 

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

Steve Guidi
But you still need to pass a reference to the instance
Jeremy
@Jeremy: caught me during an edit :)
Steve Guidi
A: 

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
+6  A: 

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 ????
    }
  }
Alex
A: 

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
Problem is that i'll have multiple instances of B that i'll be adding A items to...
Rob
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
@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
+2  A: 

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 enclosing class' constructor.

Mircea Grelus