5 Replies - 40127 Views - Last Post: 14 January 2009 - 01:33 PM Rate Topic: -----

#1 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Deep copy object?

Post icon  Posted 07 December 2008 - 03:25 AM

Hello!

How do I make a deep copy of my own class? If possible I'd like some kind of method that can be used on all of my classes. There must be an easier way than to copy the classes field by field (i hope).

I have a class that - simplified - looks like this:
Class customer
dim ID as integer
dim Fname as string
dim Lname as string
end class

' Now I want to make a copy of an object of the class customer somwhere later in my code:

dim cust1 as new customer
dim cust2 as new customer
cust1.id = 1
cust1.fname = "Jens"
cust1.lname = "Brown"

cust2 = cust1
cust2.lname = "Green"

msgbox(cust1.lname)
msgbox(cust2.lname)


The result here will be that both msgboxes will display "Green".

This is due to the fact that VB.NET does only copy the reference to the object when I write cust2 = cust1.

Copying the reference is like if you have a house and a sign that points out the house. The sign points at the house so you can find it - this is what a reference is, a sign that points out where in memory to find an object. Now, copying an object like cust2 = cust1 doesn't make a new house and a new sign, it only makes the sign cust2 point at the same house that the sign cust1 does.

A deep copy on the other hand makes a new object and copies all content from the original to the new copy. It's like building an exact copy of a house, putting it on a new lot and putting up a new sign that points to the house. (Explanation somewhat simplified here)

What if I want to make a copy of the object that cust1 holds (or points at)? What I need is a deep copy, some means to make the msgboxes in the above example display "Brown" and "Green" respectively.

Regards
/Jens

PS: Edited to delete erraneous description of deep and shallow copy. Correct (hopefully) description below.

This post has been edited by jens: 07 December 2008 - 10:03 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Deep copy object?

#2 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Deep copy object?

Posted 07 December 2008 - 09:14 AM

You have to implement deep copies for your own classes yourself. The correct way to do this is by implementing the ICloneable interface, even thou I wish they had a generic version of it. You want control over this process most of the time and it is not to hard to implement.
Was This Post Helpful? 0
  • +
  • -

#3 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: Deep copy object?

Posted 07 December 2008 - 09:26 AM

Well, I am using the terms deep and shallow copy in error above.

The phenomenon I am talking about above is the difference between copying the reference and copying the object. This means that object1 = object2 should rightly be called copying the reference while copying the object with its content to a new object should be called copying the object.

There is such a distinction as deep copy and shallow copy. It does however pertain to how the copying of the data within the object is carried out.

In short:
- A shallow copy copies all simple datatypes within the object to the new object (you have one set for each object and they are different, change one and the other is not affected) but any objects within the object being copied is not copied - only the reference to this inner object is copied - change one inner object and the other object is affected since they share the inner objects.
- A deep copy on the other hand copies all fields and all objects within the object being copied to a new object. This means that a deep copy makes a completley new object that has nothing to do with the original.

Even shorter:
- Shallow copy copies value types and copies references to reference types.
- Deep copy copies everything.

Sorry for the confusion.

Anyway, my question still stands: How do I make a deep copy of an object?

Regards
/Jens

This post has been edited by jens: 07 December 2008 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#4 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: Deep copy object?

Posted 07 December 2008 - 09:41 AM

Magicmonkey, thanks. I was writing my reply to myself while you answered.

Could you please give an example of how my simple class could use the IClonable?

I also read some stuff about making a generic method for deep copying using serialization but I didn't really catch how to. It also involved making my class serializable - somthing I really don't understand how to do right now.

Regards
/Jens
Was This Post Helpful? 0
  • +
  • -

#7 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Deep copy object?

Posted 08 December 2008 - 07:44 AM

Sure, here is an example using you simple class above. I am using .NET 3.5 With constructor to simplify things.

Public Class Form1
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim c1 As New Customer With {.ID = 1, .Fname = "Joe", .Lname = "Smith"}
		Dim c2 As Customer = c1
		Dim c3 As Customer = c1.Clone

		c2.Fname = "Jane"

		Debug.WriteLine(c1.Fname)
		Debug.WriteLine(c2.Fname)
		Debug.WriteLine(c3.Fname)
	End Sub
End Class

Class Customer
	Implements ICloneable

	Public ID As Integer
	Public Fname As String
	Public Lname As String

	Public Function Clone() As Object Implements System.ICloneable.Clone
		Return New Customer With {.ID = ID, .Fname = Fname, .Lname = Lname}
	End Function
End Class


Was This Post Helpful? 1

#8 fshost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-January 09

Re: Deep copy object?

Posted 14 January 2009 - 01:33 PM

This covers deep cloning in .NET, including the code for a generic inheritable class that will allow deep cloning of just about anything:
http://programmingco...ing-in-net.html

This post has been edited by fshost: 14 January 2009 - 01:34 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1