IDE: VS 2010, c# .net winforms
Hi, I am having 3 objects obj1 obj2 and obj3, and obj1 some values are initiated and obj2 some values are initiated and I want in obj3 as final object which will contain values of both obj1 and obj2, see below examples: (the values will be merged only if it is not null or 0.
AClass obj1 = new AClass();
obj1.value1 = 14;
AClass obj2 = new AClass();
obj2.value2 = 15;
//I want
AClass obj3 = new AClass();
obj3 = obj1 + obj2; // this is not available
//I want to get obj3.value1 = 14 and obj3.value2 = 15 (initiated)
Is there any faster or predefined way to doing this.
No, there's no built in support for merging... and unless the types of value1
and value2
are int?
, you may not be able to tell the difference between "not initialized" and "initialized to 0". (If they're properties, you could give them custom setters and remember which properties have been set that way.)
Rather than using +
, I would suggest you create your own static method to return the merged instance:
AClass obj3 = AClass.Merge(obj1, obj2);
You'd need to write the logic within that method, of course. We can't easily give sample code for that without knowing more about your requirements and types.
See more on this question at Stackoverflow