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. Collection remove

Collection remove

Scheduled Pinned Locked Moved C#
4 Posts 3 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.
  • M Offline
    M Offline
    MayukhSen
    wrote on last edited by
    #1

    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; }
        }
    
    }
    
    D 1 Reply Last reply
    0
    • M MayukhSen

      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; }
          }
      
      }
      
      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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...

      M 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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...

        M Offline
        M Offline
        MayukhSen
        wrote on last edited by
        #3

        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 case clsContentit is not removing the item from collection . Offcouse i can use base.Remove(item); instead of clear can u plz tell how i use <code>base.Remove(item);</code> and not using any index

        P 1 Reply Last reply
        0
        • M MayukhSen

          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 case clsContentit is not removing the item from collection . Offcouse i can use base.Remove(item); instead of clear can u plz tell how i use <code>base.Remove(item);</code> and not using any index

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          montosen wrote:

          can u plz tell how i use base.Remove(item); and not using any index

          Use a for loop rather than a foreach.

          "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.

          My blog | My articles | MoXAML PowerToys | Onyx

          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