-1

I have this struct contained within a

public class Class1<TKey1> : IDictionary<TKey1, Class2>
{
    #region Private Fields
    private Dictionary<Key, Class2> _class2Manager = new Dictionary<Key, Class2>(new KeyEqualityComparer());
    #endregion

    #region Key
    protected internal struct Key
    {
        private TKey1 _TKeyValue1;

        public TKey1 TKeyValue
        {
            get { return _TKeyValue1; }
            set { _TKeyValue1 = value; }
        }
        public Key(TKey1 keyValue)
        {
            _TKeyValue1 = keyValue;
        }
        ///Other Key Code
    }
    #endregion
    ///Other Class1 Code
}

I am trying to emulate (implement) the dictionary functionality of my _class2Manager dictionary within class1. The trouble arises when I want to implement the GetEnumerator() method. I am not exactly sure how to convert the IEnumerator<KeyValuePair<Key, Class2>> object returned by the _class2Manager.GetEnumerator() into a IEnumerator<KeyValuePair<TKey1, Class2>>

IEnumerator<KeyValuePair<TKey1, Class2>> IEnumerable<KeyValuePair<TKey1, Class2>>.GetEnumerator()
{
    IEnumerator<KeyValuePair<Key, Class2>> K = _class2Manager.GetEnumerator();
}

How do I convert IEnumerator<KeyValuePair<Key, Class2>> to IEnumerator<KeyValuePair<TKey1, Class2>>?

I've thought of casting, but I don't think that is the exact thing I need to do to convert it properly.

Any suggestions are appreciated, thanks.

Community
  • 1
  • 1
Maelstrom Yamato
  • 167
  • 1
  • 2
  • 7
  • 1
    Any obvious reason to reinvent the wheel? – David Mar 25 '13 at 15:59
  • What do you mean by that? – feralin Mar 25 '13 at 16:01
  • Why are you wrapping the keys in the first place? I know this is done in Java when you need to change the meaning of `equals` and `hashCode` for the key, but in .NET we have `IEqualityComparer` for that. – Sam Harwell Mar 25 '13 at 16:10
  • @David I am only showing a small portion of the code. I wouldn't say that my actual implementation is a reinvention of the wheel. There are other parts to Class1 and Key that I am not showing just so I can show the fundamental issue of my problem. – Maelstrom Yamato Mar 25 '13 at 17:10
  • @280Z28 I am using IEqualityComparer on the Key struct in a special manner which I implement elsewhere. – Maelstrom Yamato Mar 25 '13 at 17:38

2 Answers2

1

In the code for you GetEnumerator() function, you could try:

foreach (KeyValuePair<Key, Class2>> entry in _MasterFrames)
    yield return new KeyValuePair<Key1, Class2>(entry.Key.TKeyValue, entry.Value);

This, basically, will just convert each Key to a Key1 for each entry in the dictionary.

feralin
  • 3,268
  • 3
  • 21
  • 37
  • Thank you! I will have to look into the yield keyword. – Maelstrom Yamato Mar 25 '13 at 17:40
  • @MaelstromYamato the `yield` keyword is a very interesting language structure of C# that basically allows you to pause and resume execution inside a function, and return multiple values (in sequence) to a caller. It would be a good idea to look it up, and understand why/how it works. – feralin Mar 25 '13 at 18:13
0

When you want to map a sequence of objects from one type to another you can use Select:

return _class2Manager.Select(pair => 
        new KeyValuePair<Key1, Class2>(pair.Key.TKeyValue, pair.Value))
    .GetEnumerator();
Servy
  • 202,030
  • 26
  • 332
  • 449