6

I defined a class:

public class Sample{
    public string name {get;set;}
    public int price {get;set}
}

Then

 Sample sampleA = new Sample();
 sampleA.name = "test";

  Sample sampleB = new Sample();
  sampleB.price = 100;

I do this because I will save JSONed sampleA and JSONed sampleB to Azure table, to present a full sample object. In other code, sometime only need name, sometime only need price, so I only need to pull data that needed each time. Of course in real code, the structure of the sample is much much more complex.

My question is:, is there any easy method that can do:

  Sample sampleC = sampleA + sampleB;

and sampleC should contain:

 sampleC.name = "test";
  sampleC.price = 100;
Eric Yin
  • 8,737
  • 19
  • 77
  • 118

4 Answers4

6

This has actually nothing to do with partial classes. Partial class is a single class, which is "geographically" declared more than in one file.

File1.cs:

public partial class File { public string Prop2 { get;set; } }

File2.cs:

public partial class File { public int Prop1 { get;set; } }

which will, when compiled, produce:

public partial class File 
{
     public string Prop2 { get;set; } 
     public int Prop1 { get;set; } 
}

For what you are asking.
There is no such method, which would combine two different instances into one. You should write it by your own.

UPDATE:
You may ask, why there is no such method. But how would it handle situation like:

Sample sampleA = new Sample();
sampleA.name = "test";
sampleA.price = 200;

Sample sampleB = new Sample();
sampleB.price = 100;

Sample sampleC = sampleA + sampleB; // what would be the value of price here: from sampleA or sampleB?
Alexander Yezutov
  • 3,144
  • 2
  • 21
  • 23
  • Thanks, I will try just write something my own. For conflict value, first appear wins, no big deal for me, since it won't be a conflict in my design anyway – Eric Yin Dec 11 '11 at 18:54
4

How about you just overload the + operator ?

    public static Sample operator +(Sample left, Sample right)
    {
        left.price = right.price;
        return left;
    }
Gabe
  • 49,577
  • 28
  • 142
  • 181
  • 1
    This would work, but this would probably only make sense when `Sample` contains a set of nullable values. The addition could copy all values from `right` where `left` contains a null value, and vice-versa. Probably you should throw an exception when both values are set. I wouldn't overload the + operator when conceptually you are just setting price. – Steven Jeuris Dec 11 '11 at 18:16
  • 2
    @Steven Jeuris, pretty sure it's not just as simple as setting the price. This is more likely a trimmed down example just to get the concept down so the OP can apply it to their real world problem. So, the logic with the overload implementation can be much more that just simply setting the price. – Gabe Dec 11 '11 at 18:21
  • If you didn't find it worthy of mentioning in your answer, I find it worthy of mentioning it in a comment. ;p – Steven Jeuris Dec 11 '11 at 18:22
  • @Steven Jeuris, Why should I, when the OP did it for me. "Of course in real code, the structure of the sample is much much more complex." Read the question more thoroughly before you start critiquing answers. – Gabe Dec 11 '11 at 18:26
  • I'm not critiquing, I'm extending on it. Instead I [now posted my own answer](http://stackoverflow.com/a/8466402/590790). – Steven Jeuris Dec 11 '11 at 18:35
  • @Steven Jeuris, I don't feel that reiterating the OP is necessary – Gabe Dec 11 '11 at 18:51
  • Thanks, I will try just write something my own. For conflict value, first appear wins, no big deal for me, since it won't be a conflict in my design anyway – Eric Yin Dec 11 '11 at 18:55
  • @Gabe: K, then just drop the last sentence from my comment and read the first 3. – Steven Jeuris Dec 11 '11 at 18:55
  • 1
    @StevenJeuris I would overload the - operator to remove the last sentence. Conceptually they are all answers. – SliverNinja - MSFT Dec 12 '11 at 20:43
  • 1
    @SliverNinja: It was not my intention to evoke a senseless discussion like this. I was just trying to add some extra information since I found the answer to be incomplete (read: not incorrect, I didn't down vote). Adding extra relevant information in comments to notify the poster of possible improvements is as far as I know even considered good practice on SE. Of course it is his right to disagree, but he only rebutted the last sentence of my comment, which is pretty useless when not considering the other 3. – Steven Jeuris Dec 12 '11 at 22:24
2

You could accomplish this by overloading the + operator on your class. Something like this

public static Sample operator +(Sample sampleA, Sample sampleB){
    return new Sample() { Name = sampleA.Name, Price = sampleB.Price };
}
Kory
  • 31
  • 4
0

As Eric Yin mentioned in his answer, partial class is an inappropriate name to use as it refers to something else entirely.

It is possible to split the definition of a class or a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.

First of all you should consider whether using the addition operator would be a conceptually good choice. Is it obvious/uambiguous what the resulting object will be?

At first sight, I would interpret the behavior you are looking for as combining two objects by using all their non-null values. When both objects have the same field with a non-null value, how would you handle that?

Considering this is the behavior you are after, you could overload the addition operator as in Gabe's answer. When your class/struct only contains nullable values you could let the addition copy all values from one object to the other where one contains a value and the other a null value. I'd throw an exception when both objects contain a value.

As a last point, reflect on how you ended up with having to implement this behavior. It's my guess there is an underlying design issue you could address instead. You probably shouldn't use objects which only store partial data.


As another alternative the new dynamic keyword in C# also comes to mind. It might be worthwhile to check that out as well.

Community
  • 1
  • 1
Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
  • thanks. I am going to write my own overload for + and also make a new name for this situation :) I will also take a look on the dynamic keyword thing, thank you for point this out to me. – Eric Yin Dec 11 '11 at 18:57
  • 1
    @Eric Yin: Be aware that as given in your example `int` can't be null. You can make it [`Nullable`](http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx) by using `int?` instead. – Steven Jeuris Dec 11 '11 at 19:03
  • Thanks for the hint. In fact, I started write my method, and I am going to split the object in 3 parts, for 3 kind of frequent use. So I label them ABC, and will get value according to label no-matter whats the value inside. It's totally a customized solution for the customized situation about azure table :) – Eric Yin Dec 11 '11 at 19:12