Search code examples
vb.netclasstypeofenumeratorienumerator

VB.Net IEnumerator(Of Integer, MyClass) not possible?


How do I return a IEnumerator of a SortedDictionary(Of Integer, MyClass)

I have this.

Dim dictionaryTest As New SortedDictionary(Of Integer, MyClass)
Dim enumerator As IEnumerator(Of Integer, MyClass) = dictionaryTest.GetEnumerator() '<- not possible.

I want a function like

public function getTestEnumerator() as IEnumerator(Of Integer, MyClass)
   return dictionaryTest.GetEnumerator()
end function

Error generated in IDE
'System.Collections.IEnumerator' has no type parameters and so cannot have type arguments.


Solution

  • Dim dictionaryTest As New SortedDictionary(Of Integer, MyClass)
    

    Answer is replace

    Dim enumerator As IEnumerator(Of Integer, MyClass) = dictionaryTest.GetEnumerator() '<- not possible.
    

    with

    Dim enumerator As IEnumerator(Of KeyValuePair(Of Integer, Myclass)) = dictionaryTest.GetEnumerator()