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. How to remove a method from CollectionBase

How to remove a method from CollectionBase

Scheduled Pinned Locked Moved C#
questiondatabasetutorial
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.
  • R Offline
    R Offline
    Radgar
    wrote on last edited by
    #1

    How can I remove a method from System.Collections.CollectionBase? Say, I have a class: public class MyList : System.Collections.CollectionBase{} I have all the members of the collection base such as Add(object), Remove(object), RemoveAt(index), etc... I want to get rid of RemoveAt() and Insert() methods.. Do you have a solution? Thanks in advance..

    Radgar

    G P 2 Replies Last reply
    0
    • R Radgar

      How can I remove a method from System.Collections.CollectionBase? Say, I have a class: public class MyList : System.Collections.CollectionBase{} I have all the members of the collection base such as Add(object), Remove(object), RemoveAt(index), etc... I want to get rid of RemoveAt() and Insert() methods.. Do you have a solution? Thanks in advance..

      Radgar

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You can't remove methods that you inherit. You can override them with methods that throws InvalidOperationException, or you can encapsulate the collection in a class that only exposes the methods that you want.

      --- Year happy = new Year(2007);

      1 Reply Last reply
      0
      • R Radgar

        How can I remove a method from System.Collections.CollectionBase? Say, I have a class: public class MyList : System.Collections.CollectionBase{} I have all the members of the collection base such as Add(object), Remove(object), RemoveAt(index), etc... I want to get rid of RemoveAt() and Insert() methods.. Do you have a solution? Thanks in advance..

        Radgar

        P Offline
        P Offline
        Phillip M Hoff
        wrote on last edited by
        #3

        You have a couple of options: 1. Do not derive from CollectionBase and instead implement ICollection and/or IList directly. This will allow you to either not include undesirable insertion/removal methods or, where those methods are required by the collection interfaces, to throw a NotImplementedException. 2. Derive from CollectionBase but override the protected OnInsert() and OnRemove() methods and throw a NotImplementedException. (You have to be careful with OnInsert(), though, as that will also get called for Add(), which you *do* want to allow.) -Phil

        R 1 Reply Last reply
        0
        • P Phillip M Hoff

          You have a couple of options: 1. Do not derive from CollectionBase and instead implement ICollection and/or IList directly. This will allow you to either not include undesirable insertion/removal methods or, where those methods are required by the collection interfaces, to throw a NotImplementedException. 2. Derive from CollectionBase but override the protected OnInsert() and OnRemove() methods and throw a NotImplementedException. (You have to be careful with OnInsert(), though, as that will also get called for Add(), which you *do* want to allow.) -Phil

          R Offline
          R Offline
          Radgar
          wrote on last edited by
          #4

          Thank you for your responses.

          Radgar "Imagination is more important than knowledge." - Albert Einstein

          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