0

I understand VB.NET does not have yield keyword so how would you convert yield of enum. in the following code?

    private static IEnumerable<int> Combinations(int start, int level, int[] arr)
    {
        for (int i = start; i < arr.Length; i++)
            if (level == 1)
                yield return arr[i];
            else
                foreach (int combination in Combinations(i + 1, level - 1, arr))
                    yield return arr[i] * combination;
    }

Edit: This is for .NET 2.0

Any idea?

Thanks,

activebiz
  • 6,000
  • 9
  • 41
  • 64
  • I don't do VB, but you'll need to create a list to push the results into then return that. – Dave Hillier Aug 06 '13 at 10:28
  • 3
    Which version of VB are you using? VB 11 has iterator blocks... See http://msdn.microsoft.com/en-us/library/hh156729.aspx – Jon Skeet Aug 06 '13 at 10:29
  • The solution is in this question: [http://stackoverflow.com/questions/97381/yield-in-vb-net](http://stackoverflow.com/questions/97381/yield-in-vb-net) – glautrou Aug 06 '13 at 12:00

3 Answers3

2

Current versions of VB.Net do support the yield keyword. I used an automatic conversion from here to generate this code.

Private Shared Function Combinations(start As Integer, level As Integer, arr As Integer()) As IEnumerable(Of Integer)
    For i As Integer = start To arr.Length - 1
        If level = 1 Then
            yield Return arr(i)
        Else
            For Each combination As Integer In Combinations(i + 1, level - 1, arr)
                yield Return arr(i) * combination
            Next
        End If
    Next
End Function

If you can't use yield you need a list to store the results, and then return that at the end of the loop. For example:

Private Shared Function Combinations(start As Integer, level As Integer, arr As Integer()) As IEnumerable(Of Integer)
    Dim result As New List(Of Integer)()
    For i As Integer = start To arr.Length - 1
        If level = 1 Then
            result.Add(arr(i))
        Else
            For Each combination As Integer In Combinations(i + 1, level - 1, arr)
                result.Add(arr(i) * combination)
            Next
        End If
    Next
    Return result
End Function
Dave Hillier
  • 18,105
  • 9
  • 43
  • 87
  • Sorry this is for VB.NET 2.0 which does not support Yield keyword so it does not work. – activebiz Aug 06 '13 at 10:32
  • 1
    Clearly not the version you're using but it does work: http://msdn.microsoft.com/en-us/library/hh156729.aspx although I've added a variant to show how you'd do it without yield – Dave Hillier Aug 06 '13 at 10:33
  • 1
    As always with automatic converted code, the result is wrong (the converter is clearly out-dated and does not support VB.Net's iterator blocks). First of all, the function has to be marked with the `Iterator` keyword: `Private Shared Iterator Function Combinations`. Also, it's just `yield `, not `yield return `. – sloth Apr 07 '14 at 08:04
0

The solution is in this question: Yield in VB.NET.

As said above this keyword is now part of VB, look at the link if you are using an old version of VB.Net.

Community
  • 1
  • 1
glautrou
  • 3,140
  • 2
  • 29
  • 34
-1

The example that i found on internet was

C# CODE

public class List
{
//using System.Collections; 
public static IEnumerable Power(int number, int exponent)
{
    int counter = 0;
    int result = 1;
    while (counter++ < exponent)
    {
        result = result * number;
        yield return result;
    }
}

static void Main()
{
    // Display powers of 2 up to the exponent 8: 
    foreach (int i in Power(2, 8))
    {
        Console.Write("{0} ", i);
    }
}
}

/* Output: 2 4 8 16 32 64 128 256 */

VB.Net code that gives the same result as the Visual C# example.

Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim results As System.Collections.Generic.IEnumerable(Of Integer)
    results = Power(2, 8)

    Dim sb As New System.Text.StringBuilder
    For Each num As Integer In results
        sb.Append(num.ToString & " ")
    Next

    MessageBox.Show(sb.ToString)

End Sub

Public Function Power(ByVal number As Integer, ByVal exponent As Integer) As System.Collections.Generic.IEnumerable(Of Integer)

    Dim result As New List(Of Integer)

    For index As Integer = 1 To exponent
        result.Add(Convert.ToInt32(Math.Pow(number, index)))
    Next
    Return result.AsEnumerable

End Function

End Class

References --> http://msdn.microsoft.com/en-us/library/9k7k7cf0(v=vs.90).aspx

Nisarg Shah
  • 354
  • 1
  • 14