-2

Note: ALL code is hand written so syntax might be wrong I don't know

I want to merge two objects of a partial class generated by XSD2Code tool but not able to find out how.

I find this post which doesn't help either How to combine a Partial Class Object in C#? as Partial class I have has like hundred of properties and attributes. Also this code is copying not merging left.price = right.price;

Example

Public Method_1()
{ 
      FruitCrate fcA = new FruitCrate(); 
      fcA = Method_2() + Method_3(); 

}

Public FruitCrate Method_2()
{ 
FruitCrate fcB = new FruitCrate(); 
fcB.Name = ..
fcB.....  hundred of properties..

return fcB;

}

Public FruitCrate Method_3()
{ 
FruitCrate fcC = new FruitCrate(); 
fcC.Name = ..
fcC.....  hundred of properties..

return fcC;
}

This is how partial class look like,

  [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.1433")]
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
    public partial class FruitCrate{

        private List<FruitCrate> FruitCrate;

        private static System.Xml.Serialization.XmlSerializer serializer;

        public FruitCrate() {
            this.FruitCrateField = new List<FruitCrateField>();
        }

        [System.Xml.Serialization.XmlArrayAttribute(Order=0)]
        [System.Xml.Serialization.XmlArrayItemAttribute("FruitCrate", IsNullable=false)]
        public List<FruitCrate> FruitCrate{
            get {
                return this.FruitCrate;
            }
            set {
                this.FruitCrateField = value;
            }
        }
        //soo on it's a large auto generated class
Community
  • 1
  • 1
Mathematics
  • 7,314
  • 25
  • 77
  • 152
  • What do you mean by "merge"? Can you give an example of what you're trying to do and what the expected result would be? – David Aug 29 '13 at 12:44
  • 2
    From your example, it looks like you could overload the + operator. So that you could "add" FruitCrate + FruitCrate – Justin Aug 29 '13 at 12:50
  • @Justin off course I don't want that... which is why I asked question as if I do that then I will have to add 100's of lines and also it doesn't add them ! but move record from one to other – Mathematics Aug 29 '13 at 12:53
  • As in the question to which you linked, this would be an overload of the `+` operator for that class. Then within that operator overload method you'd define the logic for determining which properties are set in the resulting class (whether values are added, appended, one wins over the other, etc.) on a per-property basis. – David Aug 29 '13 at 12:53
  • vote to close as its unclear what you are asking, Justin's comment was valid imo you have shown 2 methods that return a FruitCake, and then you apply the + operator on them, this doesn't necessarily mean addition. – Sayse Aug 29 '13 at 12:54
  • 2
    You probably could have coded up half of the properties in the time it took you to ask the question. – D Stanley Aug 29 '13 at 12:56
  • @user13814: If you don't want to create a custom `+` operator then what *do* you want to do? Because the example you've shown is... using a `+` operator. If you have hundreds of properties and want to write custom logic for all of them then that will likely result in hundreds of lines of code. There's nothing inherently wrong with this. The idea is that you write all of that code once, in one place, and can re-use it through the method you've created. – David Aug 29 '13 at 12:56
  • @DStanley it's complicated just because I can't explain doesn't mean it's straight forward, look at + operator code, it's not combining properties, it's copying from one and putting in other, where as i want them to combine – Mathematics Aug 29 '13 at 13:00
  • I found my solution myself http://stackoverflow.com/questions/7504280/c-sharp-merge-objects – Mathematics Aug 29 '13 at 13:01
  • @user13814: `"it's not combining properties, it's copying from one and putting in other, where as i want them to combine"` - Then in your code you can combine them, whatever that means. (For numeric types add them together? For strings append them? For objects, do something else with them?) This would still happen in the `+` operator method. Are you asking how to add two values together? It's not complicated, and the fact that you can't explain it does indeed mean what you're asking isn't straightforward. – David Aug 29 '13 at 13:05
  • @David see my earlier comment please, I found a better way of doing it ! thanks anyway – Mathematics Aug 29 '13 at 13:05
  • @user13814: No, you found *the same* way of doing it. The only difference in what you found is that it's using a custom method name instead of the existing `+` operator. Other than that, the code is identical. It's not "better", it's the same. – David Aug 29 '13 at 13:07
  • Don't post large amounts of handwritten code. You're bound to make mistakes, especially if you aren't strong in the language (which you clearly aren't). – Kendall Frey Aug 29 '13 at 13:07
  • 1
    @David how can it be same !!!! in other way I had to write 100's of lines and this way I can do same with 2 lines of code, are you nutts ? – Mathematics Aug 29 '13 at 13:08
  • @user13814: If there's any difference then, again, you've failed to describe what it is you're actually trying to accomplish. On that question there were 2 lines of code because the object has 2 properties. If your object has hundreds of properties then there will be hundreds of lines of code. (Again, there's nothing inherently wrong with this, it's just that there are lots of properties describing your object.) Unless you don't need to "merge" all of those properties and only need to merge 2 of them... – David Aug 29 '13 at 13:11
  • @David Drink a cold glass of water mate or go on a long holiday !!, Personcars.AddRange(person.Personcars); code line solved my problem, and I didn't had to write 100 lines as suggested by all of you, cheers – Mathematics Aug 29 '13 at 13:16
  • @user13814: `AddRange` applies to a list, not any random object. In that question the list was only *one* property on the object. If you have hundreds of properties, you'll need hundreds of lines of code. If, on the other hand, you have *one* property which happens to be a list of hundreds of items then you incorrectly described your problem. In that case you don't have a large class with hundreds of properties. You have a small class with one property. – David Aug 29 '13 at 13:24
  • @David you are not understanding, I created a XSD file then used XSD 2 Code tool which generated a ten's of partial classes, now I create partial classes objects and add properties to them, please do a little of googling, I am disappointed by your argument after looking at your reputation. – Mathematics Aug 29 '13 at 13:28

2 Answers2

0

Why not implement a function to do the addition for you? For the case you mention above with only two cakes, it could look like:

public static FruitCake MergeCakes(FruitCake A, FruitCake B)
{
    FruitCake mergedCake = new FruitCake();

    // Do your merging, like for instance

    mergedCake.Price = A.Price + B.Price;

    return mergedCake;
}

You can then do your addition like this:

  FruitCrate fcA = new FruitCake(); 
  fcA = MergeCakes(Method_2(), Method_3());

If you need the ability to merge a large number of cakes, you can implement your MergeCakes-function with a List input, like for instance:

public static FruitCake MergeCakes(List<FruitCake> cakes)
{
    if(cakes != null)
    {
       FruitCake mergedCake = new FruitCake();

       // Do your merging, like for instance
       foreach(var cake in cakes)
       {
           mergedCake.Price += cake.Price;
       }
    }
    return mergedCake;
}

And then do your addition as follows:

  FruitCrate fcA = new FruitCake(); 
  fcA = MergeCakes(new List<FruitCake>(){ Method_2(), Method_3()), Method_4(), Method_5(), ... });

It might seem like I am not directly answering your question, but in my experience, you are better off keeping things as simple as possible for as long as possible. That way you can look back at your code two weeks from now and still understand whats going on.

Good luck!

Tormod Haugene
  • 3,538
  • 2
  • 29
  • 47
-1

Your question has nothing to do with partial classes. A partial class is one whose code is split between multiple files.

There's nothing "built-in" to do what you want. You could use reflection to loop through all of the properties and add any numeric values together, but you would still have to account for different numeric types (there's not a generic way to add two numeric values whose types are not known at compile-time).

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • this is what I was looking for, didn't you know about it ? http://stackoverflow.com/questions/7504280/c-sharp-merge-objects – Mathematics Aug 29 '13 at 13:05
  • @user13814: That example is doing *the exact same thing* as an overloaded `+` operator. It's just using a custom method name instead of an existing operator. – David Aug 29 '13 at 13:06