0

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.

My sample won't compile in .net 4.0

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
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 1
    and why don't you show us the code and error message. you can search for the unkown function and see if someone has converted it or written his own function – nbk Feb 15 '23 at 18:25
  • 3
    Do the answers to [Iterator pattern in VB.NET (C# would use yield!)](https://stackoverflow.com/questions/250874/iterator-pattern-in-vb-net-c-would-use-yield) help? – Andrew Morton Feb 15 '23 at 18:36
  • Does this answer your question? [Iterator pattern in VB.NET (C# would use yield!)](https://stackoverflow.com/questions/250874/iterator-pattern-in-vb-net-c-would-use-yield) – nbk Feb 15 '23 at 22:16
  • There is no equivalent. Iterators didn't exist in older versions. You just need to build the entire list first and then return it. – jmcilhinney Feb 15 '23 at 23:28

1 Answers1

2

You need the compiler for at least Visual Basic 11 (from Visual Studio 2012) to build code using iterator blocks. IIRC, the .Net 4.0 runtime itself does support this, but you need the newer compiler/build environment.

But Visual Studio 2010 is very old now and completely unsupported. Later versions are free, and can target the older runtime, so take this as a good opportunity to upgrade.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794