How to make a copy of an object

I have a collection of objects called Bookmarks that consists of a collection of Bookmarks. This collection of bookmarks is bound to the TreeView control.

I can get the bookmarks I want, but I need a copy of the bookmarks so I can work with it and not modify the original.

Any thoughts.

Thank.

+1


source to share


4 answers


Create a new constructor for the bookmark class that takes the existing bookmark as a parameter.

Inside this new constructor, copy all property values ​​from the existing bookmark to the new one.



This method is known as "Copy Constructor".

The article takes a closer look at the MSDN article, see How to Write a Copy Constructor .

+3


source


Most collection classes in .Net provide a constructor overload that allows you to pass to another collection, for example



dim copyOfBookMars as New List(of BookMark)(myOriginalBookMarkList)

      

0


source


Been with VB for a while, but C # offers a clone () method.

0


source


Usually you don't make a copy of an object, the object makes a copy of itself (a clone). Because the object contains state information, a bitwise copy cannot be considered appropriate; so the defining class must take care of this.

In your case, you can implement multiple concurrent pointers (bookmarks).

0


source







All Articles