Understanding IEnumerator in VB.NET - reset

254 views Asked by At

I'm having trouble understanding what's not happening, in my case, with IEnumerator in vb.net.

I have this code which pulls data from a database and then reads the first two rows, summing up a column from each row into 'numGirls'. Then, with 'resEnum1.Reset()', I expected 'resEnum' to reset itself to the begining of the collection, but it doesn't;

Dim resEnum1 As IEnumerator = iTrack.res_by_gender(False, YearCB.Text).GetEnumerator  
resEnum1.Reset()   
Dim resSet As Object  
Dim numGirls As Integer = 0  
For x = 0 To 1  
  resEnum1.MoveNext()  
  resSet = resEnum1.Current  
  numGirls += resSet.qty  
 Next  
 resEnum1.Reset()

At this point my code should continue and iterate through the collection starting at the begining of the collection (-1) with a MoveNext;

While resEnum1.MoveNext  
'...........'
End While  

However, the IEnumerator (resEnum1) does not infact get reset but continues from the third row of the collection. Why?
Obviously I'm not understaing something, unless it is up to me to re-invent the wheel and implement the 'reset' in code?

1

There are 1 answers

1
Bjørn-Roger Kringsjå On BEST ANSWER

Not all enumerators implement the Reset method inherited from IEnumerator. You could think of it as a "forward-only" enumerator.

"The Reset method is provided for COM interoperability. It does not necessarily need to be implemented; instead, the implementer can simply throw a NotSupportedException.". - MSDN