Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

copying elements of global lists

charlesh3charlesh3 Posts: 7Member
I'm a hardware engineer doing my first c# program.

I have 2 global lists defined in my form as:

List MemoryBank = new List(100);
List CombinedMemories = new List(100);

The first list is used by several functions. Sooner or later, I must do "garbage collection" in a different function and extract (copy) the useful elements of the first list into the second list. This works the first time, but when I do that, the references are then set to be the same. The next time I update an element in the first list, the paired entry in the second list is also updated. I don't want that.

I tried to prevent this by reading the element of the first list into a temporary (local) variable and then copying the temp into the second list, but apparently when the local variable is destroyed the elements still have the same reference.

What's the right way to fix this?

Chuck
Sign In or Register to comment.