Iterator equivalent in .net 4.0

Alec Pointer 20 Reputation points
2023-02-15T18:20:04.67+00:00

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
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,804 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Dewayne Basnett 1,371 Reputation points
    2023-02-15T18:42:21.66+00:00

  2. 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.

    0 comments No comments

  3. 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.   


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.