Custom Collection with Extension method, linq Support [modified]
-
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
-
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
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
-
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
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);
-
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);
I think you're confusing events with methods. Read the documentation again.
I know the language. I've read a book. - _Madmatt
-
I think you're confusing events with methods. Read the documentation again.
I know the language. I've read a book. - _Madmatt
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
-
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
The problem is you are trying to create something that already exists
I know the language. I've read a book. - _Madmatt
-
The problem is you are trying to create something that already exists
I know the language. I've read a book. - _Madmatt
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
-
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
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)
-
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)
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
-
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
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.