Recent Posts

Monday, May 14, 2012

How to clone an Object in VB.Net without changing values in the parent object when new values are changed?

How to clone an object change the values of the properties of the object. To clone you can create the class as IClonable

For eg:

Public Class person     Implements ICloneable     Public Function Clone() As Object Implements System.ICloneable.Clone         Return MyBase.MemberwiseClone()     End Function     Private iID As Integer     Public Property ID As Integer         Get             Return iID         End Get         Set(ByVal value As Integer)             iID = value         End Set     End Property End Class ' Now outside the class you can do following to copy the object         Dim per1 As New person         per1.ID = 1         Dim per2 As New person         per2 = per1.Clone()
So if you do per2.id = 3 then it will not change the value in per1 so that mean you have distinctly independent object to work around.

Related Posts by Categories




1 comment:

Anonymous said...

I ADAMS KEVIN, a representative Aiico Insurance plc, we trust and respect for individual differences in day out a loan. We will provide 2% of the loan's interest rate. If you are interested in this business contact us by e-mail: (adams.credi@gmail.com) now transfer their loan documents issued properly. Do you need a loan to set up business or school if you are very welcome to Aiico Insurance plc. You can also contact us by e-mail: (adams.credi@gmail.com). We first week can request a balance transfer.

DO YOU NEED LOAN FOR PERSONAL BUSINESS? IF YOU CONTACT YOUR EMAIL ABOVE TO PROCEED WITH YOUR LOAN TRANSFER IMMEDIATELY OK.

Post a Comment