Take the 2-minute tour ×

I have a collection of custom objects which are mirrored in the UI

Interface (extremely simplified)

public interface IMyInterface
  string Name { get; set; }
  int Data { get; set; }


MyCollection<IMyInterface> Foo;

UI (which displays contents of Foo in a sensible way, e.g.)

Name: [ asdf ] Data: [ 1234 ]
Name: [ qwer ] Data: [ 5678 ]
Name: [ zxcv ] Data: [ 0000 ]

I now have the need in my program to be able to open two IMyInterface types and display the average data, along with single IMyInterface objects:

Name: [ asdf ] Data: [ 1234 ]
Name: [ qwer ] Data: [ 5678 ]
Name: [ zxcv ] Data: [ 0000 ]
Name: [ 2 Objects (asdf, qwer) ] Data: [ 3456 ]

I must be able to get out asdf and qwer from this new average.

What is the best way I can achieve this? I feel I have three options:

  1. Make a type which inherits from IMyInterface with an internal collection of IMyInterfaces. However, this feels a bit dirty as some of the properties are not averageable.
  2. Replace Foo with `MyCollection> and where ever I use it, make a check to the count of the inner collection
  3. Turn MyCollection into a dictionary-like object where I can specify a "group" for each object then I can get out objects by their group ID.

Any suggestions welcome.

share|improve this question
What determines which items are averaged? You omitted zxcv in your example. –  Ek0nomik Oct 8 '12 at 13:38
winforms? wpf? asp? what UI technology are you using? –  MattDavey Oct 8 '12 at 13:40
@zxcv It would be user choice to determine which items are averaged (in my actual application this is done by ListView - a user may activate a single item or select multiple items to be activated). –  dav_i Oct 8 '12 at 13:40
@MattDavey The UI isn't really as important as the underlying collection in this case, but it is WinForms. –  dav_i Oct 8 '12 at 13:41
@dav_i I think it's quite vital to point out what UI technology you're using, as something like WPF would have opened up a lot more options on how to tackle something like this. –  MattDavey Oct 8 '12 at 13:43
show 7 more comments

1 Answer

up vote 2 down vote accepted

You can use the Composite design pattern, a good sample of which is the controls tree in .net:

Each IMyInterace can have a collection of IMyInterfaces, and its your choice what to do with the irrelevant properties (you can always return a null)

share|improve this answer
edited - "composite" pattern, not "composition" –  Tom Bushell Oct 8 '12 at 14:45
yup, thanks for correcting –  YavgenyP Oct 8 '12 at 15:24
add comment

Your Answer


