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

مشاهدة النسخة كاملة : List element copying problem



C# Programming
11-12-2009, 12:32 AM
A few months ago some of you helped this hardware engineer with a c background understand that list elements are passed by reference. I solved my immediate problem but have run into another wall.

I have two global lists defined in my form:

List <CUserMem> MemoryBank = new List<CUserMem>(100);
List <CUserMem> CombinedMemories = new List<CUserMem>(100);

After a while, the first list becomes has out of date info and I must "garbage collect" by copying the still-needed elements of the first list to the second list. I simply do :

CombinedMemories[Index].whateverelement = MemoryBank[AnotherIndex].whateverelement;

It works the first time through but of course the next time I update MemoryBank, CombinedMemories is also updated. I tried to avoid this by:

local (temporary) variable = MemoryBank[AnotherIndex].whateverelement;
CombinedMemories[Index].whateverelement = local (temporary) variable;

That doesn't work. After the local variable is destroyed, the two list elements still have the same reference.

So what is the correct way to work with 2 global lists like this?? Can I create a copy of the first list that is not "linked" (have the same reference)???


Thanks -

Chuck


Chuck