INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How to get a List item without reference

How to get a List item without reference

(OP)
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

RE: How to get a List item without reference

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]

RE: How to get a List item without reference

(OP)
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

RE: How to get a List item without reference

(OP)
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

RE: How to get a List item without reference

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.

RE: How to get a List item without reference

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close