-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
18
  • 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? Commented Aug 29, 2013 at 12:44
  • 2
    From your example, it looks like you could overload the + operator. So that you could "add" FruitCrate + FruitCrate Commented Aug 29, 2013 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 Commented Aug 29, 2013 at 12:53
  • 2
    You probably could have coded up half of the properties in the time it took you to ask the question. Commented Aug 29, 2013 at 12:56
  • 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 ? Commented Aug 29, 2013 at 13:08

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

Comments

-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).

2 Comments

this is what I was looking for, didn't you know about it ? stackoverflow.com/questions/7504280/c-sharp-merge-objects
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.