List with ListChanged and ListChanging events - asking for code review and suggestions (long source code included)
-
Hi! This is my first question in this board. So please be gentle with your comments I just wanted a List class that provides events when the list is about to change or has already changed. I briefly looked in the framework documentation and searched the internet but I haven't found anything useful. I would greatly appreciate any comments or information on best practise solutions for this requirement. Thanks in advance! My implementation:
public class ObservedList<T> : List<T>
{
public ObservedList()
{} public ObservedList(IEnumerable<T> collection) : base(collection) { } public ObservedList(int capacity) : base(capacity) { } #region EventArgs public enum ChangeAction { Add, Remove, Clear, Sort } public class ObservedListChangingEventArgs : ObservedListChangedEventArgs { public ObservedListChangingEventArgs(ChangeAction action, List<T> item) : base(action, item) { } private bool \_cancel; public bool Cancel { get { return \_cancel; } set { \_cancel = value; } } private bool \_handled; public bool Handled { get { return \_handled; } set { \_handled = value; } } }
-
Hi! This is my first question in this board. So please be gentle with your comments I just wanted a List class that provides events when the list is about to change or has already changed. I briefly looked in the framework documentation and searched the internet but I haven't found anything useful. I would greatly appreciate any comments or information on best practise solutions for this requirement. Thanks in advance! My implementation:
public class ObservedList<T> : List<T>
{
public ObservedList()
{} public ObservedList(IEnumerable<T> collection) : base(collection) { } public ObservedList(int capacity) : base(capacity) { } #region EventArgs public enum ChangeAction { Add, Remove, Clear, Sort } public class ObservedListChangingEventArgs : ObservedListChangedEventArgs { public ObservedListChangingEventArgs(ChangeAction action, List<T> item) : base(action, item) { } private bool \_cancel; public bool Cancel { get { return \_cancel; } set { \_cancel = value; } } private bool \_handled; public bool Handled { get { return \_handled; } set { \_handled = value; } } }
At a first glance: You are both inheriting the List class and declaring another internal list as _items. That means that you are using two different lists, one for the base methods that use the list itself, and one for the methods overriding the base methods and using the _items list instead. --- b { font-weight: normal; }
-
At a first glance: You are both inheriting the List class and declaring another internal list as _items. That means that you are using two different lists, one for the base methods that use the list itself, and one for the methods overriding the base methods and using the _items list instead. --- b { font-weight: normal; }
Okay, I must admit this looks a bit strange but I've encapsulated the EventArg classes into the main class so the _items object is in fact not in the main class but in a EventArg class. Maybe I should extract those _sub_classes