Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get a List item without reference

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
0
0
US
I am trying to get the 1st (or second) item from a list. I don't want the reference as I want to change a couple of fields before adding it back in and I don't want to change the original.

For example:
Code:
List<SomeClass> myClass = new List<SomeClass>();

myClass.Add(...);
myClass.Add(...);

SomeClass newClass;

newClass = myClass[0];    // this creates a pointer

newClass.name = "Joe";

I don't want myClass[0].name to be "Joe"

I don't want to clone the whole list, just one of the list items.

Thanks,

Tom
 
This is the C/C++ programmers nightmare when moving to C#. If you look up, C# deep copy there are lots of hits on how to do this.

The main thing is that the class must be marked as [Serializable]
 
Why would it have to be serializable if it is moving from one type to the same type?

Also, most of what I see is moving a list to another list. I haven't found one that shows how to move an item of the list to another item of the same type.

I also want to move it back into the list (which at this point can be a reference).

I just don't want to create a new type and have to copy each value from the list item to the new item when I can just copy it.

Thanks,

Tom
 
I found it.

I can implement the ICloneable interface on the class I am using, create a Shallow copy method that calls MemberwiseClone() and call that.

Since I am not concerned with reference values in the this class, it works fine.

Thanks
 
Basically depends on what sort of copy you want. As you said, ICloneable is for a shallow copy. For a deep copy you need it to be serializable.

The problem with the serializable/cloneable methods is you can make an instance without going through the constructor so it may miss some vital initializations. Just tread carefully when using them.
 
The essential truth is, that myClass[0] is a reference, so when you change it you automatically also change the list item myClass[0] in the list. The list item itself only is a reference to the original object memory location, too.
The only way not to modify this object is to first make a copy. Whether you need a deep or shallow copy is a different matter, but you need a new instance. To have a separate pointer to the same memory doesn't make it a seperate object.

I learned that the other way around: Using an inital instance I set its fields to data coming from a database and aded each final item to a list, I ended up with a list of all items pointing to the same object, only its final state was saved many times (of course).

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top