Collection remove
-
Hi i have created a generic collection . I have My custom class. When i am adding objs of my custom class in the collection .I am not able to Search remove from the collection even i contains() method is not working .Can u plz Tell me where i am wrong. Or How should i do this.
public class GCommon<T> : Collection<T> where T : ICommonInterface
{new public void Add(T item) { if(!base.Contains(item)) base.Add(item); } public void Clear(T item) { foreach (T it in base.Items) { if(it.Equals(item)) base.Remove(item); } } new public bool Contains(T item) { bool retval = false; foreach (T itm in base.Items) if (itm.Equals(item)) { retval = true; break; } return retval; } } public interface ICommonInterface { } public class clsContent : ICommonInterface { public clsContent() { } public clsContent(int id,string Name, string Content) { \_Id = id; \_ContentName = Name; \_Content = Content; } private int \_Id; public int Id { get { return \_Id; } set { \_Id = value; } } private string \_ContentName; public string ContentName { get { return \_ContentName; } set { \_ContentName = value; } } private string \_Content; public string Content { get { return \_Content; } set { \_Content = value; } } }
-
Hi i have created a generic collection . I have My custom class. When i am adding objs of my custom class in the collection .I am not able to Search remove from the collection even i contains() method is not working .Can u plz Tell me where i am wrong. Or How should i do this.
public class GCommon<T> : Collection<T> where T : ICommonInterface
{new public void Add(T item) { if(!base.Contains(item)) base.Add(item); } public void Clear(T item) { foreach (T it in base.Items) { if(it.Equals(item)) base.Remove(item); } } new public bool Contains(T item) { bool retval = false; foreach (T itm in base.Items) if (itm.Equals(item)) { retval = true; break; } return retval; } } public interface ICommonInterface { } public class clsContent : ICommonInterface { public clsContent() { } public clsContent(int id,string Name, string Content) { \_Id = id; \_ContentName = Name; \_Content = Content; } private int \_Id; public int Id { get { return \_Id; } set { \_Id = value; } } private string \_ContentName; public string ContentName { get { return \_ContentName; } set { \_ContentName = value; } } private string \_Content; public string Content { get { return \_Content; } set { \_Content = value; } } }
montosen wrote:
public void Clear(T item) { foreach (T it in base.Items) { if(it.Equals(item)) base.Remove(item); } }
You cannot remove an item from a collection while enumerating over the collection with a
foreach
. Youd would have to use an index into the collection instead. Also, the name you picked for this method, "Clear", would tell me that the method removes all items from the collection, not just a single element. "Remove" would be a better choice. Why are you even implementing this Clear (Remove) method when the base Collection class already implements it?A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
montosen wrote:
public void Clear(T item) { foreach (T it in base.Items) { if(it.Equals(item)) base.Remove(item); } }
You cannot remove an item from a collection while enumerating over the collection with a
foreach
. Youd would have to use an index into the collection instead. Also, the name you picked for this method, "Clear", would tell me that the method removes all items from the collection, not just a single element. "Remove" would be a better choice. Why are you even implementing this Clear (Remove) method when the base Collection class already implements it?A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...if i am using a collection of int,string or any system defined data type
base.Remove(item);
is removing the item but when i am a custom class in this caseclsContent
it is not removing the item from collection . Offcouse i can usebase.Remove(item);
instead of clear can u plz tell how i use <code>base.Remove(item);</code> and not using any index -
if i am using a collection of int,string or any system defined data type
base.Remove(item);
is removing the item but when i am a custom class in this caseclsContent
it is not removing the item from collection . Offcouse i can usebase.Remove(item);
instead of clear can u plz tell how i use <code>base.Remove(item);</code> and not using any indexmontosen wrote:
can u plz tell how i use
base.Remove(item);
and not using any indexUse a
for
loop rather than aforeach
."WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.