tags:

views:

207

answers:

4

I have a collection of an object called Bookmarks which is made up of a collection of Bookmarks. This collection of bookmarks are bound to a treeview control.

I can get the bookmarks back out that I need, but I need a copy of the bookmarks so I can work with it and not change the original.

Any thoughts.

Thanks.

A: 

Most collection classes in .Net provide a constructor overload that allow you to pass in another collection like

dim copyOfBookMars as New List(of BookMark)(myOriginalBookMarkList)
Micah
It is not a list it is a single Bookmark that I need a copy of that has a property that is a list of Bookmarks. Dim aBookmark As New FalconBookmark
So i can't use this, I need a way to clone my Original Bookmark, or break the link to the orginal.
A: 

Been awhile with VB, but c# offers a clone() method.

steve_mtl
The problem with Clone() is that the semantics are poorly defined - is it a shallow or a deep copy? Better approach is to use a copy constructor.
Bevan
That same argument also applies to a copy constructor.Is the copy constructor a shallow copy or a deep copy?So, I wouldn't use that as a basis for your decision which way to go.Defining a method called clone() has some advantages over a copy constructor.A method can be polymorphic ...
Scott Wisniewski
... This means that if you have a hierarchy of classes, that you can have an override of Clone in each class that can create something different.You can't do that with a copy constructor. Using "new Foo" always creates a Foo.If you don't need polymorphism, then it's really just about style ....
Scott Wisniewski
... Do you like var copy = new Foo(original);orvar copy = original.Clone();
Scott Wisniewski
I typically use var copy = original.Clone(), but now I am reconsidering and will be a little more selective.Great string of follow up comments!
steve_mtl
A: 

You don't generally make a copy of an Object, an Object makes a copy of itself (clone). Since an object contains state inforrmation, a bitwise copy can't be counted on as being appropriate; so the defining class needs to take care of it.

You may want to implement multiple simultaneous pointers (bookmarks) in your case.

le dorfier
+2  A: 

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

Within this new constructor, copy all the property values from the existing bookmark onto the new one.

This technique is known as a "Copy Constructor".

There's an article on MSDN that goes into more detail - see How to Write a Copy Constructor.

Bevan