Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. List with ListChanged and ListChanging events - asking for code review and suggestions (long source code included)

List with ListChanged and ListChanging events - asking for code review and suggestions (long source code included)

Scheduled Pinned Locked Moved C#
questioncode-review
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Patrick Klug
    wrote on last edited by
    #1

    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; }
            }
        }
    
    G 1 Reply Last reply
    0
    • P Patrick Klug

      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; }
              }
          }
      
      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

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

      P 1 Reply Last reply
      0
      • G Guffa

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

        P Offline
        P Offline
        Patrick Klug
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups