Results 1 to 2 of 2

Thread: Disable CollectionBase.RemoveAt

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2005

    Disable CollectionBase.RemoveAt

    I have a class MyCollection that inherits CollectionBase. One of the methods of CollectionBase is CollectionBase.RemoveAt(index as integer). MyCollection doesn't support RemoveAt so i want to override CollectionBase.RemoveAt but the function is declared NotOverridable. Same problem with .Clear. How can i solve this?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: Disable CollectionBase.RemoveAt

    If you want to override a method that is not overridable then you have to shadow it. Shadowing is similar to overriding but not the same. You should read about the two to see what the differences are. For instance, if you were able to override RemoveAt, if someone was to cast a variable of your type as CollectionBase and then call RemoveAt it would be your implementation that was called. If you shadow the method and someone casts a variable of yuor type as CollectionBase then calling RemoveAt will invoke the implementation from the CollectionBase class.

    Another option is to not inherit CollectionBase. You could define your own class that wraps an ArrayList and doesn't expose those members. That could be a problem though because the IList interface requires you to implement the Clear and RemoveAt methods, so if your class doesn't have those methods then you cannot use it where an IList is required, like for data-binding. Having implementations of those methods that do nothing may also lead to counter-intuitive behaviour is situations like data-binding.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width