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?

[2905 byte] By [JimStapleton] at [2007-12-16]
# 1
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.

MarcD at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2
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.

JimStapleton at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3


#region "Using Directives"
using System;
using System.Collections;
using System.Collections.Specialized;
#endregion
namespace CompanyName.Internal
{
#region "Summary - Description for Class1"
/// <summary>
/// Summary description for Class1.
/// </summary>
#endregion
public class Class1 : IDictionary
{
#region "Constructors"
public Class1()
{
}
#endregion
#region IDictionary Members
public bool IsReadOnly
{
get
{
// TODO: Add Class1.IsReadOnly getter implementation
return false;
}
}
public IDictionaryEnumerator GetEnumerator()
{
// TODO: Add Class1.GetEnumerator implementation
return null;
}
public object this[object key]
{
get
{
// TODO: Add Class1.this getter implementation
return null;
}
set
{
// TODO: Add Class1.this setter implementation
}
}
public void Remove(object key)
{
// TODO: Add Class1.Remove implementation
}
public bool Contains(object key)
{
// TODO: Add Class1.Contains implementation
return false;
}
public void Clear()
{
// TODO: Add Class1.Clear implementation
}
public ICollection Values
{
get
{
// TODO: Add Class1.Values getter implementation
return null;
}
}
public void Add(object key, object value)
{
// TODO: Add Class1.Add implementation
}
public ICollection Keys
{
get
{
// TODO: Add Class1.Keys getter implementation
return null;
}
}
public bool IsFixedSize
{
get
{
// TODO: Add Class1.IsFixedSize getter implementation
return false;
}
}
#endregion
#region ICollection Members
public bool IsSynchronized
{
get
{
// TODO: Add Class1.IsSynchronized getter implementation
return false;
}
}
public int Count
{
get
{
// TODO: Add Class1.Count getter implementation
return 0;
}
}
public void CopyTo(Array array, int index)
{
// TODO: Add Class1.CopyTo implementation
}
public object SyncRoot
{
get
{
// TODO: Add Class1.SyncRoot getter implementation
return null;
}
}
#endregion
#region IEnumerable Members
IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
// TODO: Add Class1.System.Collections.IEnumerable.GetEnumerator implementation
return null;
}
#endregion
}
}


This compiles fine for me. P.S If you are using any version of Visual Studio lower then 2005, you may want to check out Whole Tomato's plugin

MarcD at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 4
Wow...
The descriptions for the various this[] interfaces did *not* properly describe the return type. Thanks.
JimStapleton at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 5
Did the code sample solve the problem?
MarcD at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 6

Jim Stapleton wrote:
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).

It's an explict interface implementation. (See the C# spec, section 13.4.1)

-Shawn

ShawnFarkas-MS at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 7
With the VS2k5 refactoring tools when your class implements an interface and you use the little smart tag there are 2 options. Implement methods implicitly or Implement methods explicitly. The latter is useful for situations such as these.
MarcD at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified