See https://visualstudiomagazine.com/articles/2011/11/10/understanding-iterators-part-1.aspx
Iterator equivalent in .net 4.0
Hi,
Anyone knows about the Iterator equivalent in .net 4.0?
I have a ( Iterator Function / Yield ) code sample which will not compile in my .NET fw 4.0 project.
Need to change it to work in vs.net 2010 .net 4.0, any idea how to proceed?
Thanks.
Private Shared Iterator Function AllNodes(ByVal nodes As NodeCollection) As IEnumerable
For i As Integer = 0 To nodes.Count - 1
Dim node As Node = nodes(i)
Yield node
If node.Nodes.Count > 0 Then
For Each item As Node In AllNodes(node.Nodes)
Yield item
Next item
End If
Next i
End Function
3 answers
Sort by: Most helpful
-
-
Bruce (SqlWork.com) 72,841 Reputation points
2023-02-15T20:51:53.9766667+00:00 there is no support yield generators in that version of vb. (it mostly compiler sugar).
the easiest fix would be to convert the function to return an array or collection of nodes. instead of yield, add the node to the collection.
-
Jiachen Li-MSFT 33,866 Reputation points Microsoft External Staff
2023-02-16T01:35:19.26+00:00 Hi @Alec Pointer ,
According to Iterators (Visual Basic), Iterators were introduced in Visual Basic in Visual Studio 2012.
It is recommended that you turn to newer versions of Visual Studio.
Or you might consider putting all Nodes into an array.
Best Regards.
Jiachen Li
----------
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.