Supposing I have this code
public class Class1
{
List<Class2> myClass2List = new List<Class2>;
public Class1() { }
}
public class Class1List : List { }
public class Class2
{
public Class2() { };
//Class2 objects will sit in a list that is an attribute of a Class1 object
// class1 objects will be in a list that is an attribute of a Class3 object
// is it possible for this method to access the list in the Class3 object
// without passing the list as a parameter.
public void myClass2Method(){ }
}
public class Class3
{
Class1List myList = new Class1List();
public Class3(){ }
static void Main()
{
Class3 myClass3Var = new Class3();
// do something to fill myList;
// added some code to maybe make it a bit less muddy
foreach (Class1 c1 in this.Class1List){
foreach (class2 c2 in c1.myClass2List{
// here is where I want to reference the Class1List attribute of the
/// Class3 object - do I need to pass the list as a paramter?
c2.MyClass2Method();
}
}
}
}
Now supposing I have an instance of Class2 in myList in the instance of Class3. Is it possible for the Class2 method myClass2Method() to access myList wouthout passing the list as a parameter?
You can, but you dont want to.
you can add a member to class2 of type Class1List and when you add the instances of class2 to mylist you can set said member to mylist and than use the local member in the function.
BUT doing so is bad for you, dont do it, your deisgn is wrong and you need to ask a different question (how to design _____).