0

I am using a dictionary wrapper class, and I want to iterate through it using key-value pairs as seen below

private void LoadVariables(LogDictionary dic)
{
    foreach (var entry in dic)
    {
        _context.Variables[entry.Key] = entry.Value;
    }

}

but a NotImplementedExceptionis thrown because I did not implement the GetEnumerator() method.

Here is my wrapper class:

public class LogDictionary: IDictionary<String, object>
{
    DynamicTableEntity _dte;
    public LogDictionary(DynamicTableEntity dte)
    {
        _dte = dte;
    }
        bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
    {
        throw new NotImplementedException();
    }

    IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}
Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
Marwan Tushyeh
  • 1,505
  • 4
  • 24
  • 47
  • possible duplicate of [How would you implement the IEnumerator interface?](http://stackoverflow.com/questions/53967/how-would-you-implement-the-ienumerator-interface) – Hamlet Hakobyan Jan 29 '13 at 13:32

3 Answers3

4

Assuming you have no special logic that your wrapper needs during enumeration you just forward the calls on to the contained instance:

public class LogDictionary: IDictionary<String, object>
{
    DynamicTableEntity _dte;
    public LogDictionary(DynamicTableEntity dte)
    {
        _dte = dte;
    }
        bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
    {
        throw new NotImplementedException();
    }

    IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
    {
        return _dte.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
dkackman
  • 15,179
  • 13
  • 69
  • 123
3

You're going to need to implement a List or Dictionary internally to hold the values of your LogDictionary.

Without knowing what DynamicTableEntity is I will assume it implements IDictionary<string,object>.

public class LogDictionary: IDictionary<String, object>
{
    private IDictionary<String, object> _dte;

    public LogDictionary(DynamicTableEntity dte)
    {
        _dte = (IDictionary<String, object>)dte;
    }

    bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
    {
        return _dte.Remove(item.Key);
    }

    IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
    {
        return _dte.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
1

Maybe you should derive from Dictionary (not IDictionary) and call base, instead of throwing an exception in the method.

Stephan
  • 1,639
  • 3
  • 15
  • 26
  • It cannot act as a wrapper for `DynamicTableEntity` then, so that doesn't answer the question. –  Jan 29 '13 at 13:37