?
Solved

C# Copy Properties from one Object to Another.

Posted on 2012-08-22
3
Medium Priority
?
722 Views
Last Modified: 2012-08-22
Hi Experts,
i have a class as below
public class Employee
{
public int ID{get;set;}
public string Name{get;set;}
}
Employee emp = new employeee{id =1 NAme = 'aaa'}

Employee  emp1 = emp;
emp1.name = "BBB";

I do not want  emp's name property also to be changed to "BBB";

How can i achieve this ,  Using reflection do i need to copy properties from one object to other or is there a better way , please advice.
0
Comment
Question by:B_Pati
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1500 total points
ID: 38321535
Here's your problem:  you haven't created two objects. You've created one object with two references pointing to it. Even reflection wouldn't help you here.

My suggestion would be to create a "copy" constructor that can initialize the object accordingly. For example:

public Employee(Employee template)
{
    this.ID = template.ID;
    this.Name = "N/A";
}

Open in new window


Then you could do:

Employee emp = new Employeee{id =1 NAme = 'aaa'}

Employee  emp1 = new Employee(emp);
emp1.name = "BBB";

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38321590
The copying of all the values to the new instance can be accomplished with Reflection, but I don't know that it's worth it unless you have a ~huge~ number of properties that you don't want to list out manually.  The number of lines of code will probably end up being around the same, except that you have to use a bunch of brain power use the Reflection approach and then you need to worry about bugs...
0
 
LVL 1

Author Closing Comment

by:B_Pati
ID: 38321811
Thanks kaufmed: This help me with out writing reflection code.
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question