Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Generic List<> change events

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike D Sutton - 19 Mar 2008 14:53 GMT
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
Jon Skeet [C# MVP] - 19 Mar 2008 14:59 GMT
<snip>

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

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Mike D Sutton - 19 Mar 2008 17:32 GMT
> 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
Jon Skeet [C# MVP] - 19 Mar 2008 18:13 GMT
> > Have you looked at these types?
> >  System.ComponentModel.BindingList<T>
[quoted text clipped - 7 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Mike D Sutton - 19 Mar 2008 19:00 GMT
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.