Generic List<> change events

M

Mike D Sutton

I'm frequently using generic lists to represent child lists in my current system, and often require a bi-directional
link between them (so a child knows it's parent as well as the parent having the list of children.)
Currently I'm providing a "new" implementation of Add() and Remove(), which set and null the parent link on the child
object respectively, which is ok but a bit tedious for every parent-child relationship in the system. In addition to
this, the other functions which manipulate the list items (Insert, InsertRange, RemoveAt, RemoveAll etc) will allow list
access without managing the child's parent property.

What I'm really after is a generic list type that would expose either some events, or virtual methods that can be
overridden to add additional functionality when adding or removing elements. Probably along the lines of:

***
interface IHaveAParent<T> { T Parent { get; set; } }
abstract class CParentList<T> : List<T> where T : IHaveAParent<CParentList<T>> {
// Manage parent properties
public virtual void OnAddOne(T item) { item.Parent = this; }
public virtual void OnAddList(IEnumerable<T> list) {
foreach (T item in list) item.Parent = this; }
public virtual void OnRemoveOne(T item) { item.Parent = null; }
public virtual void OnRemoveList(IEnumerable<T> list) {
foreach (T item in list) item.Parent = null; }

public new void Add(T item) {
OnAddOne(item);
base.Add(item);
}

public new void Remove(T item) {
OnRemoveOne(item);
base.Remove(item);
}

// Other "new" methods here...
}

class CExampleChildItem : IHaveAParent<CParentList<CExampleChildItem>> {
#region IHaveAParent<CParentList<CExampleChildItem>> Members
private CParentList<CExampleChildItem> m_Parent;

public CParentList<CExampleChildItem> Parent {
get { return m_Parent; }
set { m_Parent = value; }
}
#endregion
}

class ExampleList : CParentList<CExampleChildItem> { /* Stuff */ }
***

I just really wanted to check that I'm not missing some GenericListWithChangeNotifications<> class somewhere which
already offers this functionality before I go and write my own, or is there some other way of achieving the desired
functionality?
Cheers,

Mike
 
J

Jon Skeet [C# MVP]

I just really wanted to check that I'm not missing some
GenericListWithChangeNotifications<> class somewhere which
already offers this functionality before I go and write my own, or is
there some other way of achieving the desired
functionality?

Have you looked at these types?
System.ComponentModel.BindingList<T>
System.Collections.ObjectModel.ObservableCollection<T>
 
M

Mike D Sutton

Have you looked at these types?
System.ComponentModel.BindingList<T>
System.Collections.ObjectModel.ObservableCollection<T>

Looks like System.Collections.ObjectModel.ObservableCollection<T> is .NET 3.0+, while I'm still stuck in .NET 2.0, but
System.Collections.ObjectModel.Collection<T> looks like it should work pretty well for what I need - thanks!
Is it be possible to add back in the Find(Predicate) and Sort(Comparison) methods without too much hacking? They're not
essential, but would come in handy for a couple of situations.
Cheers,

Mike
 
J

Jon Skeet [C# MVP]

Mike D Sutton said:
Looks like System.Collections.ObjectModel.ObservableCollection<T> is
.NET 3.0+, while I'm still stuck in .NET 2.0, but
System.Collections.ObjectModel.Collection<T> looks like it should
work pretty well for what I need - thanks!
Is it be possible to add back in the Find(Predicate) and
Sort(Comparison) methods without too much hacking? They're not
essential, but would come in handy for a couple of situations.

Find is pretty trivial to write - just apply the predicate to each
element in turn until you find a match.

Sort requires a bit more work, i.e. a real sort algorithm. Far from
impossible, but probably not worth doing until you actually need them.
 
M

Mike D Sutton

Sort requires a bit more work, i.e. a real sort algorithm. Far from
impossible, but probably not worth doing until you actually need them.

Sort happens very infrequently at the moment and generally on pretty short lists (20 or so items), so for the time being
I guess I could simply create a List<T> internally, copy everything to it, perform the sort there then move everything
back. Bit on the ugly side, but cheap and cheesy for now until I get time to port in a Comparison based QuickSort.
Thanks again,

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top