Home

"Yield" in VB?

Using the latest version of LINQPad 5 Free (v5.10.00), I wrote a VB Program that included the Yield statement (see https://msdn.microsoft.com/en-us/library/hh156729.aspx).

But Yield shows up in red, with the error "BC30451: 'Yield' is not declared. It may be inaccessible due to its protection level."

Sample program to illustrate:
Sub Main Dim colors As New List(Of String) From { "Red", "Orange", "Yellow", "Green", "Blue" } For Each letter As String In GetFirstLetters(colors) letter.Dump() Next End Sub Function GetFirstLetters(stringList As List(Of String)) As List(Of Char) For Each givenString As String In stringList Yield givenString.Chars(0) Next End Function

Any settings I should be checking? Or just avoid Yield with LINQPad?

Comments

  • My bad. Reading further in the MSDN article I referenced, "Yield" is not a reserved word and has special meaning only when it is used in an Iterator function or Get accessor.

    Changing the second function's code to the below solved it:
    Iterator Function GetFirstLetters(stringList As List(Of String)) As IEnumerable(Of Char) For Each givenString As String In stringList Yield givenString.Chars(0) Next End Function
Sign In or Register to comment.