Implementing an IDictionary class
I am working on a class, that implements IDictionary, and the problem is, IDicitionary requires one type of GetEnumerator, and it implements ICollection, requireing a different type of enumerator.
I have three options, each generates it's own little error:
The most obvious implementation (it gives an error complaining about two implementations of GetEnumerator differing only by return type)
| |
publicclass Configuration : IDictionary { /* implemention required, but [most likely] irrelevant code */ publicoverride IDictionaryEnumerator GetEnumerator() { System.Collections.IDictionaryEnumerator e =new Configuration.enumerator(this); } public IEnumerator GetEnumerator() { System.Collections.IEnumerator e =new Configuration.enumerator(this); } }
|
So, I try the IDictionary method, and it complains that System.Collections.IEnumerable's variant of GetEnumerator isn't present (wrong return type).
| |
publicclass Configuration : IDictionary { /* implemention required, but [most likely] irrelevant code */ publicoverride IDictionaryEnumerator GetEnumerator() { System.Collections.IDictionaryEnumerator e =new Configuration.enumerator(this); } }
|
So, I provide option 3, and it complains that IDictionarie's variant of IEnumerator isn't present (wrong return type)
| |
publicclass Configuration : IDictionary { /* implemention required, but [most likely] irrelevant code */ public IEnumerator GetEnumerator() { System.Collections.IEnumerator e =new Configuration.enumerator(this); } }
|
Suggestions for the fix?
I think what you need to do, This is alot easier to see in VS 2005 because the refactoring tools let you "implicitly implement interface methods" or "explicitly implement interface methods", but you are looking for public IEnumerator IEnumerator.GetEnumerator and public IDictionary
| |
IEnumerator System.Collections.IEnumerable.GetEnumerator() { // TODO: Add Class1.System.Collections.IEnumerable.GetEnumerator implementation return null; } public IDictionaryEnumerator GetEnumerator() { // TODO: Add Class1.GetEnumerator implementation return null; } |
The namespace in the method name allows multiple instances of the same method name to exist. These methods are then called by typecasting. For example if for some reason you need the IEnumerator youwould go ((IEnumerator)MyDictClass).GetEnumerator
hope this helps.
Thanks, What is that type of "double-declaration" called in the help (I'm using 2002, so the section numbers may not be the same).
edit:
I am apparantly still missing something as I am getting another error that appears to be related. The solution and/or the topic I should look up to figure out why I'm having these issues would be great (the latter especially).
This set of code gives me an error:
D:\My Documents\Visual Studio Projects\FConfig\Class1.cs(157): 'FConfig.Configuration' does not implement interface member 'System.Collections.IDictionary.this[object]'. 'FConfig.Configuration.this[object]' is either static, not public, or has the wrong return type.
| |
public System.Collections.IDictionary this [object pos] { get { /*lots of code here...*/ } set { /*and even more code here*/ } } |
So I try this, and get an error:
D:\My Documents\Visual Studio Projects\FConfig\Class1.cs(349): Identifier expected, 'this' is a keyword
| |
public System.Collections.IDictionary.this [object pos] { get { /*lots of code here...*/ } set { /*and even more code here*/ } } |
All I can think to try next is this, but it too produces an error:
D:\My Documents\Visual Studio Projects\FConfig\Class1.cs(349): The modifier 'public' is not valid for this item
| |
public System.Collections.IDictionary System.Collections.IDictionary.this [object pos] { get { /*lots of code here...*/ } set { /*and even more code here*/ } } |
So I drop the public...
D:\My Documents\Visual Studio Projects\FConfig\Class1.cs(349): 'this' in explicit interface declaration is not a member of interface
| |
System.Collections.IDictionary System.Collections.IDictionary.this [object pos] { get { /*lots of code here...*/ } set { /*and even more code here*/ } } |
so, I drop the "Sytem.Collections.IDictionary." attached to the "this", not able to think of anything else, and it doesn't throw an error for that section of code, but I'm back to an earlier error:
D:\My Documents\Visual Studio Projects\FConfig\Class1.cs(157): 'FConfig.Configuration' does not implement interface member 'System.Collections.IDictionary.this[object]'. 'FConfig.Configuration.this[object]' is either static, not public, or has the wrong return type.