Synchronizing a ListDictionary

A

Allen Jones

Hi,

I tried to extend ListDictionary in order to create a thread safe version
named SynchronizedListDictionary.

Despite the docs saying the properties and methods of ListDictionary are
virtual, when I try to compile my derived class, the compiler tells me that
I "cannot override inherited member XXX because it is not marked virtual,
abstract, or override".

I know there are other collections and techniques that I can use as an
alternative, but I want to understand how a method can be listed as virtual
in the docs (which I thought came straight from the source) but the compiler
doesnt recognize it as such.

Thanks

Regards
Allen
 
J

Jon Davis

Try using "new". It doesn't override the virtual method, but it will be
called when referenced in the context of your customized class.

Jon
 
A

Allen Jones

Hi Jon,

Thanks for the input, but as I said in my post, I am not after work arounds
or alternatives. I want to understand why this does not work the way it
should.

Regards
Allen
 
A

Austin Ehlers

Hi,

I tried to extend ListDictionary in order to create a thread safe version
named SynchronizedListDictionary.

Despite the docs saying the properties and methods of ListDictionary are
virtual, when I try to compile my derived class, the compiler tells me that
I "cannot override inherited member XXX because it is not marked virtual,
abstract, or override".

I know there are other collections and techniques that I can use as an
alternative, but I want to understand how a method can be listed as virtual
in the docs (which I thought came straight from the source) but the compiler
doesnt recognize it as such.

Thanks

Regards
Allen

How did you override it? Using VS2003, this works:
public class MyDict:ListDictionary,IDictionary
{
public MyDict()
{
}
private class myDict:IDictionary
}


When you type myDict:IDictionary, VS will show a little tip that says:
"Press TAB to implement stubs for interface
System.Collections.IDictionary"

Does that work for you?
 
A

Allen Jones

Austin,

Its the individual members that cause a problem when I compile them. e.g.

public class SynchronizedListDictionary : ListDictionary {

// other stuff

public override void Add(object key, object value) {
// method implementation
}
}

In the example the Add method causes the compiler to report the message:
Add(object, object) canot override
System.Collections.Specialized.ListDictionary.Add(object,object) because it
is not marked virtual, abstract, or override.

The problem is, according to the docs, ListDictionary.Add IS virtual.

Regards
Allen
 
S

Simon Trew

Yes, I have tried it & I agree there seems to be a discrepancy between the
docs and real life.

I appreciate you are not looking for workarounds, but just for information,
you can override the interface explicitly i.e.:

void IDictionary.Add(object key, object value);

However, this may not be ideal in all circumstances.

S.
 
A

Austin Ehlers

Well. looking at the rotor source code, it does appear that the
documentation is wrong. It's declared as:

public void Add(object key, object value)
{
//stuff
}

So, it's another error in the documentation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top