Hello,
I want to implement the iterator pattern in VB.net, which does not have the yield
keyword.
Any ideas or links please?
Hello,
I want to implement the iterator pattern in VB.net, which does not have the yield
keyword.
Any ideas or links please?
Hmm, looks like you might be out of luck:
I was struggling with an issue today when converting some C# to VB.NET. C# has a really cool "yield return" statement that is used in an iterator block to provide a value to the enumerator object. VB.NET does not have the "yield" keyword. So, there are a few solutions (none of which are really clean) to get around this. You could use a return statement to return the value if you are looping through and would like to break an enumerator and return a single value. However, if you'd like to return the entire enumeration, create a List() of the child type and return the list. Since you are usually using this with an IEnumerable, the List() will work nice.
That was written a year ago, not sure if anyone has come up with anything else better since then..
VB.net does not support the creation of custom iterators and thus has no equivalent to the C# yield keyword. However you might want to look at this KB article for more information.
C#'s yield keyword forces the compiler to create a state machine in the background to support it. VB.Net does not have the yield keyword. But it does have a construct that would allow you to create a state machine within a function: Static function members.
It should be possible to mimic the effects of a yield return function by creating a generic class that implements IEnumerable as well as the needed state machine and placing an instance as a static member inside your function.
This would, of course, require implementing the class outside of the function. But if done properly the class should be re-usable in the general case. I haven't played with the idea enough to provide any implementation details, though.
Still getting my head around this concept also.
The article 'Use Iterators in VB Now' was recently published in Visual Studio Magazine.
http://visualstudiomagazine.com/columns/article.aspx?editorialsid=2972