0

I need to create a custom collection that implements IBindingList in order to be able to bind it with a custom control from 3rd party. Another problem that I have is that I have to make that collection thread safe because I manually insert items from multiple threads simultaneously.

Anyways I am using a BindingList<T> as a field on my class in order to don't reinvent the wheel to much. So my class looks like:

class ThreadSaveBindingCollection<T> : IEnumerable<T>, IBindingList
{
    BindingList<T> collection;
    object _lock = new object();

    // constructors
    public ThreadSaveBindingCollection(IEnumerable<T> initialCollection)
    {
        if (initialCollection == null)
            collection = new BindingList<T>();
        else                            
            collection = new BindingList<T>(new List<T>(initialCollection));                                 
    }
    public ThreadSaveBindingCollection() : this(null)
    {            
    }   

    // Todo: Implement interfaces using collection to do the work    
}

Note I am missing to implement the interface IEnumerable and IBinding list. I am planning for the field collection to take care of that as it implements those interfaces as well. So I let visual studio implement the interface explicitly and replace the throw new NotImplementedException() with the field collection implementation and I end up with something like:

enter image description here

Now the question is

Why I cannot call the method AddIndex on the field collection if collection claims to implement IBindingList!?

enter image description here

I am not able to do the same thing for several of the methods

2
  • It looks like your second code block is missing. Can you make sure all of the relevant code is in your question? Commented Nov 23, 2012 at 16:38
  • Cast to IBindingList, then call. Commented Nov 23, 2012 at 16:44

3 Answers 3

1

It's because it's an explicit implementation of the interface, rather than implicit. This means you must call it through the interface rather than the type itself. For example:

((IBindingList)collection).AddIndex(property);

See here for more information on explicit interface implementations.

Sign up to request clarification or add additional context in comments.

Comments

1

It is implemented explicity by BindingList, you need to cast your reference of collection to IBindingList to use it:

(collection as IBindingList).AddIndex(property);

http://msdn.microsoft.com/en-us/library/ms132679.aspx

Further reading.

The point of explicit implementation and access via a reference of the interface itself is to resolve naming conflicts where two parties create two separate interfaces with the same method signatures - it allows you to disambiguate the methods whilst still implementing both interfaces.

Comments

1

BindingList explicitly implements IBindingList, so you need to do

(collection as IBindingList).AddIndex(property);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.