user38349
user38349

Reputation: 3025

How do you make a copy of an Object

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.

Upvotes: 1

Views: 1953

Answers (4)

Bevan
Bevan

Reputation: 44307

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.

Upvotes: 3

dkretz
dkretz

Reputation: 37645

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.

Upvotes: 0

steve_mtl
steve_mtl

Reputation: 970

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

Upvotes: 0

Micah
Micah

Reputation: 116070

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)

Upvotes: 0

Related Questions