VB.NET's "yield return"

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.

Published Wednesday, October 17, 2007 11:12 AM by Jason N. Gaylord
Filed under:

Comments

# re: VB.NET's "yield return"

Wednesday, October 17, 2007 2:26 PM by Speednet

If the function returns IEnumerable(Of String) your return statement (which is the equivalent of yield return "MyReturnValue") would be:

Return New String() {"MyReturnValue"}

Or, instead of String it could be any enumerable type.

# re: VB.NET's "yield return"

Thursday, October 18, 2007 1:32 PM by Jason N. Gaylord

Right

# Feed Search Engine - All Fresh Articles And News Are Here

Sunday, November 25, 2007 7:09 PM by Feed Search Engine - All Fresh Articles And News Are Here

Pingback from  Feed Search Engine - All Fresh Articles And News Are Here

# re: VB.NET's "yield return"

Wednesday, December 31, 2008 9:18 AM by Chris

I'm new to .NET technology and came across the code below. However I don't know how to apply the suggestions you above to the following code:

public static IEnumerable<string> ReadLinesFromFile(string filename)

{

   using (StreamReader reader = new StreamReader(filename))

   {

       while (true)

       {

           string s = reader.ReadLine();

           if (s == null)

               break;

           yield return s;

       }

   }

}

********

var products = from line in ReadLinesFromFile(@"c:\import.txt")

              let item = line.Split('\t')

              select new

              {

                  Product = item[0],

                  Quantity = Convert.ToInt32(item[1]),

                  Price = Convert.ToDecimal(item[2]),

                  Total = Convert.ToInt32(item[1]) * Convert.ToDecimal(item[2])

              };GridView1.DataSource = products;

GridView1.DataBind();

*******

Please help in converting C# to VB. Thanks!

# re: VB.NET's "yield return"

Monday, January 19, 2009 4:59 AM by VB Programmer

Public Shared Function ReadLinesFromFile(ByVal fileName As String) As IEnumerable(Of String)

       Dim stringList As New List(Of String)

       Using reader As StreamReader = New StreamReader(fileName)

           Dim s As String

           While True

               s = reader.ReadLine

               If s Is Nothing Then

                   Exit While

               Else

                   stringList.Add(s)

               End If

           End While

       End Using

       Return stringList

   End Function

# re: VB.NET's "yield return"

Monday, January 19, 2009 9:25 AM by Paul Irwin

That works if you're returning an IEnumerable(Of T), but not if you're returning an IEnumerator(Of T). In that case, return your list but tack on the GetEnumerator() function to the end. i.e. Return myList.GetEnumerator()

# re: VB.NET's "yield return"

Sunday, March 29, 2009 1:15 AM by ahK

isnt that defeating the purpose of "yield return" for lazy evaluating.

Leave a Comment

(required) 
(required) 
(optional)
(required)