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. .NET (Core and Framework)
  4. Custom Collection with Extension method, linq Support [modified]

Custom Collection with Extension method, linq Support [modified]

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpcsharpdatabaselinqtutorial
10 Posts 3 Posters 1 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.
  • A Offline
    A Offline
    anand kumar m
    wrote on last edited by
    #1

    Hi I am creating custom collection object that inherits arraylist, ienumerable, etc. Because I can't go for Generic list since, that doesn't have event support (insert, add, remove, etc...) I need some events to be raised on item add/remove/insert on list public class CustomList<T> : ArrayList, IBindingList , IArrayList<T> this supports linq, if I use object type in the linq query var aa = (from User a in users where a.IsDirty == false select a).ToList(); The problem, i could not use extension methods (.Where, etc..) public class CustomList<T> : ArrayList, IBindingList , IArrayList<T>,IEnumerable<T>, IEnumerable public new IEnumerator<T> GetEnumerator() { return this.GetEnumerator(); } But this doesn't filter the objects in the list. for example users.Where(a=>a.isDirty == true); Also if I inherit Ienumerable I get 'System.StackOverflowException' exception. Please help Anand

    modified on Thursday, April 15, 2010 2:55 AM

    N G 2 Replies Last reply
    0
    • A anand kumar m

      Hi I am creating custom collection object that inherits arraylist, ienumerable, etc. Because I can't go for Generic list since, that doesn't have event support (insert, add, remove, etc...) I need some events to be raised on item add/remove/insert on list public class CustomList<T> : ArrayList, IBindingList , IArrayList<T> this supports linq, if I use object type in the linq query var aa = (from User a in users where a.IsDirty == false select a).ToList(); The problem, i could not use extension methods (.Where, etc..) public class CustomList<T> : ArrayList, IBindingList , IArrayList<T>,IEnumerable<T>, IEnumerable public new IEnumerator<T> GetEnumerator() { return this.GetEnumerator(); } But this doesn't filter the objects in the list. for example users.Where(a=>a.isDirty == true); Also if I inherit Ienumerable I get 'System.StackOverflowException' exception. Please help Anand

      modified on Thursday, April 15, 2010 2:55 AM

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      What do you mean generic list doesn't support Add, Remove or Insert? http://msdn.microsoft.com/en-us/library/3wcytfd1(v=VS.100).aspx[^] http://msdn.microsoft.com/en-us/library/sey5k5z4(v=VS.100).aspx[^] http://msdn.microsoft.com/en-us/library/cd666k3e(v=VS.100).aspx[^]


      I know the language. I've read a book. - _Madmatt

      A 1 Reply Last reply
      0
      • N Not Active

        What do you mean generic list doesn't support Add, Remove or Insert? http://msdn.microsoft.com/en-us/library/3wcytfd1(v=VS.100).aspx[^] http://msdn.microsoft.com/en-us/library/sey5k5z4(v=VS.100).aspx[^] http://msdn.microsoft.com/en-us/library/cd666k3e(v=VS.100).aspx[^]


        I know the language. I've read a book. - _Madmatt

        A Offline
        A Offline
        anand kumar m
        wrote on last edited by
        #3

        list supports add, removve, insert . But I think it doesnt support events public interface IArrayList<T> { int Add(T value); void Clear(); //bool Contains(T value); //int IndexOf(T value); void Insert(int index, T value); void Remove(T value); void RemoveAt(int index);

        N 1 Reply Last reply
        0
        • A anand kumar m

          list supports add, removve, insert . But I think it doesnt support events public interface IArrayList<T> { int Add(T value); void Clear(); //bool Contains(T value); //int IndexOf(T value); void Insert(int index, T value); void Remove(T value); void RemoveAt(int index);

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          I think you're confusing events with methods. Read the documentation again.


          I know the language. I've read a book. - _Madmatt

          A 1 Reply Last reply
          0
          • N Not Active

            I think you're confusing events with methods. Read the documentation again.


            I know the language. I've read a book. - _Madmatt

            A Offline
            A Offline
            anand kumar m
            wrote on last edited by
            #5

            my issue is not events/methods, how to incorporate the linq, extention support in to custom list. public class CustomList<T> : ArrayList, IBindingList , IArrayList<T>,IEnumerable<T>, IEnumerable { public new IEnumerator<T> GetEnumerator() { return this.GetEnumerator(); } } 1. var aa = (from User a in users where a.IsDirty == false select a).ToList(); this works, but public new IEnumerator<T> GetEnumerator() throws stackoverflow exception. 2. users.Where(a=> a.IsDirty == false); this always returns empty/null

            N 1 Reply Last reply
            0
            • A anand kumar m

              my issue is not events/methods, how to incorporate the linq, extention support in to custom list. public class CustomList<T> : ArrayList, IBindingList , IArrayList<T>,IEnumerable<T>, IEnumerable { public new IEnumerator<T> GetEnumerator() { return this.GetEnumerator(); } } 1. var aa = (from User a in users where a.IsDirty == false select a).ToList(); this works, but public new IEnumerator<T> GetEnumerator() throws stackoverflow exception. 2. users.Where(a=> a.IsDirty == false); this always returns empty/null

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              The problem is you are trying to create something that already exists


              I know the language. I've read a book. - _Madmatt

              A 1 Reply Last reply
              0
              • N Not Active

                The problem is you are trying to create something that already exists


                I know the language. I've read a book. - _Madmatt

                A Offline
                A Offline
                anand kumar m
                wrote on last edited by
                #7

                linq, extension methods work after casting the objects. public class CustomList<T> : ArrayList, IBindingList , IArrayList<T> I removed IEnumerable from the customList users.Cast<User>().Where(a=> a.IsNew).ToList() var aa = (from User a in users where a.IsDirty == false select a).ToList();

                modified on Friday, April 16, 2010 12:59 AM

                1 Reply Last reply
                0
                • A anand kumar m

                  Hi I am creating custom collection object that inherits arraylist, ienumerable, etc. Because I can't go for Generic list since, that doesn't have event support (insert, add, remove, etc...) I need some events to be raised on item add/remove/insert on list public class CustomList<T> : ArrayList, IBindingList , IArrayList<T> this supports linq, if I use object type in the linq query var aa = (from User a in users where a.IsDirty == false select a).ToList(); The problem, i could not use extension methods (.Where, etc..) public class CustomList<T> : ArrayList, IBindingList , IArrayList<T>,IEnumerable<T>, IEnumerable public new IEnumerator<T> GetEnumerator() { return this.GetEnumerator(); } But this doesn't filter the objects in the list. for example users.Where(a=>a.isDirty == true); Also if I inherit Ienumerable I get 'System.StackOverflowException' exception. Please help Anand

                  modified on Thursday, April 15, 2010 2:55 AM

                  G Offline
                  G Offline
                  Gideon Engelberth
                  wrote on last edited by
                  #8

                  It sounds like you want to inherit from Collection(Of T) or ObservableCollection(Of T) (Note that the page is for .NET 4. ObservableCollection is in the WindowsBase assembly in .NET 3.5)

                  A 1 Reply Last reply
                  0
                  • G Gideon Engelberth

                    It sounds like you want to inherit from Collection(Of T) or ObservableCollection(Of T) (Note that the page is for .NET 4. ObservableCollection is in the WindowsBase assembly in .NET 3.5)

                    A Offline
                    A Offline
                    anand kumar m
                    wrote on last edited by
                    #9

                    Yes, Collection is better choice. protected override void RemoveItem(int index) {} Collection doesn't have RemoveItem(object item) method to override.... so couldn't raise remove event, on removing by object..... any fix for this? Thanks in Advance Anand

                    G 1 Reply Last reply
                    0
                    • A anand kumar m

                      Yes, Collection is better choice. protected override void RemoveItem(int index) {} Collection doesn't have RemoveItem(object item) method to override.... so couldn't raise remove event, on removing by object..... any fix for this? Thanks in Advance Anand

                      G Offline
                      G Offline
                      Gideon Engelberth
                      wrote on last edited by
                      #10

                      If all you want to do is raise an event, you should probably just use ObservableCollection, which does raise events on add/insert, remove, and setting items. The Remove(T item) will call the protected overridable RemoveItem(int index) after figuring out the proper index, that's why only one is overridable.

                      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