Amresh S

Amresh S

  • NA
  • 437
  • 17k

How to Clone a Class instance without reference?

Oct 27 2016 8:16 AM
Hi All,
 
I'm trying to clone a class instance, which have rich collection of internal property values. Theses data are fetched from the database. I want to clone this instance in which the changes made in that instance will not affect the real one time created instance. Please refer the below code snippet:
 
  1. public class DB_Data  
  2.     {  
  3.         public DB_Data Data  
  4.         { getinternal set; }  
  5.           
  6.         public DB_Data()  
  7.         { }  
  8.   
  9.         public void DB_Schema()  
  10.         {  
  11.             // code to get data from DataBase;  
  12.         }  
  13.     }  
 Now, I want a copy/clone of the instance which contains the DataBase data. My problem is, when I assign a new set of values to the instance, the referred instance value gets affected. Since the info fetched form the data base is a complex process, I don't want the copy variable to be initialized with real-data base info by repeated function call to the method DB_Schema(). is there any way to do this?
 
Kindly share your ideas. Thanks in advance.
 
Regards,
Amresh S. 

Answers (2)