C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Duplicating Object with Clone


Posted Date: 03 Nov 2004    Resource Type: Articles    Category: .NET Framework
Author: Custom SoftwareMember Level: Bronze    
Rating: 1 out of 5Points: 5



Introduction
--------------------------
--------------------------
Paragraph Heading
--------------------------
' Just added to show the difference between shallow and deep cloning
_
Class SampleRefType
Public Val As Integer
End Class

_
Class Employee

Public m_Name As String
Public m_age As Int16
Public someObject As SampleRefType

' Do shallow cloning
Function ShallowClone() As Employee
Return CType(Me.MemberwiseClone(), Employee)
End Function

' This is one way to do deep cloning. But works only if the
' objects and its references are serializable
Function DeepClone() As Employee
Dim m As New MemoryStream()
Dim b As New BinaryFormatter()
b.Serialize(m, Me)
m.Position = 0
Return b.Deserialize(m)
End Function

Sub Print()
Console.WriteLine("Name : " & m_Name)
Console.WriteLine("Age : " & m_age)
Console.WriteLine("Some Value: " & someObject.Val)
End Sub

End Class

Module MainModule

Sub Main()
Dim objRef As New SampleRefType()
objRef.Val = 1000

Dim objA As New Employee()
objA.m_Name = "customsoftware"
objA.m_age = 23
objA.someObject = objRef

Dim objB As Employee = objA.DeepClone
Dim objC As Employee = objA.ShallowClone

'objB is a deep clone. So chages made to objRef would not affect objB's value.
'But objC is shallow cloned. So, if the objRef is changed, so is the value in
'ObjC. So objC should print 2000 whereas objB should print 1000
objRef.Val = 2000

'Updating some state
objC.m_age = 100
'objB.m_Name = "New Name"
objA.m_Name = "aaa"

Console.WriteLine("Original Object:")
objA.Print()
Console.WriteLine()
Console.WriteLine("Shallow Cloned Object:")
objC.Print()
Console.WriteLine()
Console.WriteLine("Deep Cloned Object:")
objB.Print()
Console.ReadLine()

End Sub

End Module
--------------------------
Paragraph Heading
--------------------------
--------------------------
Summary
--------------------------
--------------------------



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Drop down form
Previous Resource: Retrieving HTML content from a given URL by making a web request
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use