Reputation: 1357
This is an excellent video explaining what it is and the differences, but it seems it has a fundamental flaw, you can't skip around the list; it even lacks a Previous() method.
I was sending a list off to a function after if it found a match, instead of the original for...next starting from the next index, i just returned the row of where the next match was found in the function, as it has already done a secondary loop to read all the lines between each 'Hello' block.
I can't do any of this with an iteration class like IEnumerator; am i missing something?
For row As Integer = 0 To dataList.Count - 1
If row <> -1 Then
If dataList.Item(row) = "Hello" Then row = SayHello(row)
End If
Next
Upvotes: 0
Views: 107
Reputation: 185663
The purpose of the interface (and the accompanying IEnumerable
interface) is to provide a forward-only mechanism for iterating over a collection (or similar structure). Given that, I don't think it's fair to say that it has a "fundamental flaw" because you cannot skip around or go in reverse any more than it would be fair to say that a compact car has a fundamental flaw because you can't use it to transport large farm animals.
Or, for those who may not favor car analogies, it's akin to saying a banana has a fundamental flaw because it (in an ordinary, unfrozen state) cannot be used to pound in nails.
Upvotes: 0
Reputation: 1785
The point of an IEnumerator is to only serve ONE object at a time, which means you can save memory and/or time. To work with a list, you need to fill the list first. Even if you do not really need the complete list, but always use a single item at each iteration.
Private Iterator Function AllIntegers() As IEnumerable(Of Integer)
Dim i = Integer.MinValue
While i < Integer.MaxValue
Yield i
End While
End Function
Now you could do
For each i in AllIntegers
And now try to put all these integers into a List(Of Integer)
Upvotes: 0
Reputation: 524
The very definition of enumerating is to mention things one by one. To enumerate is to start at the top and look at each item down the list. That is what enumerating is. So, something that is enumerable would be something that you could look at one by one from the top. The name IEnumerable is very appropriate.
Upvotes: 0
Reputation: 499132
you can't skip around the list
You say that's a flaw. It isn't when you don't want to allow skipping around a list.
The number one thing people do with lists is iterate over them - this is the lowest common denominator for collections of all types.
If you do need to skip around, as in your example, don't use IEnumerator
.
Upvotes: 2