views:

58

answers:

2

I have a class, say DerivedBindingList<T>, which is derived from BindingList<T>.

I would like to use an indexer with the derived class, and have coded it as:

        public T this[int index]
        {
            get
            {
                // Getter code
            }
            set
            {
                // Setter code
            }
        }

However, the compiler complains with the following message: "...hides inherited member 'System.Collections.ObjectModel.Collection.this[int]'. Use the new keyword if hiding was intended."

I can add the 'new' keyword and the compiler is happy, but should I be doing things differently in some way to avoid this warning?

Perhaps I have to use base.this[] somehow?

Thanks.

+2  A: 

The indexer in BindingList isn't virtual, so you can't override it - you'll have to just hide it if you really want to do this.

I don't think I'd advise it though - member hiding is a recipe for confusing code. What are you trying to do? Do you definitely want to derive from BindingList<T> instead of composing it (i.e. having a member of your class of type BindingList<T>)? What is your new indexer going to do?

Jon Skeet
I am trying to return a filtered or non-filtered set of results from the list, and as such have implemented IBindingListView and associated filtering code. The name of my class is actually 'FilteringBindingList<T>'.
Andy
A: 

This warning shows that an indexer already exists in base class.If you want to change its behavior you should either override it (if it's defined as virtual in base class) or use new keyword to tell the compiler to use derived indexer method whenever it's working with an instance of derived class.

Beatles1692