Friday, April 26, 2013

Copying values among Objects


I was writing a code when Chris Gale was batting. Century in just 30 balls, that’s some awesome batting man! So you can imagine the importance of that code.
Well that code was all written to copy the values of one object to another, property by property. The good thing is, the two objects need not to be same! Another good thing is we do not have to even bother what are the properties that those object possesses. Amazed! ain’t you? Let me show.

When we talk about classes, we are talking about something which is reference type, means when you create two instances of that class (inc1 and inc2, say,) and initialize second instance with first (inc2 = inc1), the two instances will now be sharing a common memory location. That is to say, if you change the value of instance one (inc1 in our case), the instance two (inc2) will automatically starts reflecting that change. It is something like you and your friend, both writing in one paper, no matter who writes, the content will change for both of you.

This referencing is good sometimes, as it saves memory, but situations do exists when you need to intentionally create two separate objects of the same class, still you want both the objects to have same properties value. You can’t simply just equate the two, it will create reference. There is a way though, in which you equate two objects by individually assigning properties of one to another. But situation can go even worse if the object have hundreds of properties to work with. Assigning hundreds of properties is not a really good idea, don’t you think?

What next? Well there is a solution. What if you can, somehow, get all the properties of the objects in collections. Then you can take one property of the source object, search same property in the destination object, then assign the value of the property of the source object to the same property of the destination object. Good idea, ain’t it? So, lets get started. Following is the function that does exactly the same as we’ve discussed above.

   1: public void CopyObject(object source, object destination)
   2: {
   3:    var propertyOfDestinationObject = destination.GetType().GetProperties();
   4:    var propertyOfSourceObject = source.GetType().GetProperties();
   5:  
   6:    foreach (var itemSource in propertyOfSourceObject)
   7:    {
   8:        foreach (var itemDestination in propertyOfDestinationObject)
   9:        {
  10:            if (itemDestination.Name.Equals(itemSource.Name))
  11:            {
  12:                itemDestination.SetValue(destination, itemSource.GetValue(source, new object[] {}),
  13:                                                                                  new object[] {});
  14:                break;
  15:            }
  16:        }
  17:    }
  18: }

 

As you can see, we have created the list of all the properties of source and destination objects viz propertyOfSourceObject and propertyOfDestinationObject. Grabbed one property from source and loops through the entire properties of destination, to search for that specific property. Once find, assigned the value of the source property to the destination property. This process will continue until all the properties of the source has value copied to their counterparts in the destination. Once completed, bang! we are done copying.

Share this post

1 comments

  1. Really a nice helper. When in RIA you can also write this method in a new module & adorn it to make an extension method. We should give that a try sometimes. :)

    ReplyDelete

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top