المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : adding to a list



C# Programming
06-02-2009, 04:51 PM
Hi all,
I am quite new to C# and I bumped into this issue and I cannot understand why it is happening. I have a list of objects of class1. class1 has, as a member variable a list of objects of class2, and class2 has as a member variable a list of objiects of class3. In my code I have to add some objects to the list of objects of class3 for the last element of the list of objects of class2 for one of the elements of the list of objects of class1. It seems it should be straight forward but i found a strange behaviour and maybe you clever people can help me make some sense out of it.
I'll explain with an example: if the list of objects of class1 has 2 elements (ListClass1[0] and ListClass2[1]) and I do


ListClass1[0].ListClass2[ListClass1[0].ListClass2.Length-1].ListClass3.Add(ObjClass3)


at the end of this I find that the ObjClass3 has been added to ListClass1[1].ListClass2[ListClass1[1].ListClass2.Length-1].ListClass3

Why is that happening?
Is there any way around this?
Cheers
Mauri