2

i have a list property like that

protected IList<System.Windows.Media.Color> colors;    
public IList<System.Windows.Media.Color> Colors
{
     get { return colors; }
     set { colors = value; }
}

and i have a function

protected void updateBuffers()

which needs to be called each time a property user change the property, for example

Colors.Add(...)
...
Colors.Clear(...)

is there an elegent simple way to do that?

Dela
  • 38
  • 1
  • 6
  • You wanto call the function everytime your colors change? – Andrei Neculai Jul 12 '13 at 11:52
  • I will point you, When object is added to a list, the function .Add is running for the collection, you should override it, refer http://stackoverflow.com/questions/1626049/overriding-listts-add – ilansch Jul 12 '13 at 11:52
  • Try making a Customized Generic method of List.Add method which inputs the list as parameter and after that run your desired method i.e updateBufffers(). for e.g ListAdd(Colors); public void ListAdd(IList List){ List.Add(..); updateBuffers(); } – sohaiby Jul 12 '13 at 11:54
  • 1
    Don't use List<>, you cannot override its methods. The best class to use is `Collection`. Works just like List<> but it has protected methods you can use to detect items getting added or modified. – Hans Passant Jul 12 '13 at 12:12

2 Answers2

4

You could create a new object that created additional properties, using the List as a base class:

public class CustomList<T> : List<T>
{
    public new void Add(T item) {
        base.Add(item);
        this.UpdateBuffers();
    }
}

The 'new' keyword is required to completely overwrite the existing implementation of Add, which isn't marked as virtual in the base class.

Thanks to Hans Passant and LarsTech for their feedback in the comments.

Adrian Wragg
  • 7,311
  • 3
  • 26
  • 50
  • 3
    Please use a C# compiler to test your code before you post it. You'll discover the very basic flaw in your answer, the List<>.Add() method is not virtual so cannot be overridden. – Hans Passant Jul 12 '13 at 12:07
  • Hmya, that doesn't work either of course. The client programmer will just use Add() as normal, or use the IList methods, and it will fail to work in a completely undiagnosable way. You've got the answer with the most votes, it's your burden to make it a useful answer :) At least talk about the Collection<> class. Or delete your answer. – Hans Passant Jul 12 '13 at 12:49
  • @HansPassant this makes you wonder about the whole vote system – ilansch Jul 12 '13 at 13:42
  • @HansPassant Wouldn't `public new void Add(T item) {...}` work? – LarsTech Jul 12 '13 at 14:13
  • 1
    @Lars - no, still fails miserably when the client programmer uses an IList reference to the collection. It will call the hidden method. – Hans Passant Jul 12 '13 at 14:35
  • @HansPassant Ah, had a feeling I was missing something. – LarsTech Jul 12 '13 at 14:38
2

You cannot with a plain list. You can however create your own class implementing IList<T> or inherit from Collection<T>.

Or you can use an ObservableCollection<T> or any built-in class implementing INotifyCollectionChanged

see MSDN for documentation.

Moreover, ObservableCollection will allow you to use your collection in bindings if you are using them for populating UI in WPF or Windows 8 applications.

Falanwe
  • 4,636
  • 22
  • 37