-3

Good day,

I have a little issue with converting a function from C# to VB.NET. Here's the code:

    public static IEnumerable<string> Lexicograph(List<string> characters, int length)
    {
        for (int i = 0; i < characters.Count; i++)
        {
            if (length == 1)
                yield return characters[i];

            else
                foreach (string nxt in Lexicograph(characters.GetRange(i + 1, characters.Count - (i + 1)), length - 1))
                    yield return characters[i] + " " + nxt;
        }
    }

I know there are some online convertors, but those aren't doing their job correctly since the compiler says there are errors/warnings in my code (conversion from String to IEnumerable).

I'm not that familiar with the lists and the IEnumerable interface and that's why I came here.

If there's anyone who can find a solution for this or any other kind of tip, it'll be greatly appreciated.

Thanks in advance.

000
  • 26,951
  • 10
  • 71
  • 101
  • 1
    You haven't shown any code to show what you have tried so far. SO is not a translation service. – Daniel Kelley Aug 22 '13 at 09:24
  • 2
    We're not here to do conversion for you. You'd probably get more assistance if you post the converted code and highlight where the errors are arising. – Damien_The_Unbeliever Aug 22 '13 at 09:24
  • VB.NET doesn't provide iterator blocks. See my old question here: http://stackoverflow.com/questions/3811589/translation-of-yield-into-vb-net (**Edit** seems to be supported now in VS 2012: http://msdn.microsoft.com/en-us/library/vstudio/dscyy5s0(v=vs.110).aspx) – Tim Schmelter Aug 22 '13 at 09:25

3 Answers3

1

If you're using Visual Studio 2012:

Public Shared Iterator Function Lexicograph(characters As List(Of String), length As Integer) As IEnumerable(Of String)
    For i = 0 To characters.Count - 1
        If length = 1 Then
            Yield characters(i)
        Else
            For Each nxt In Lexicograph(characters.GetRange(i + 1, characters.Count - (i + 1)), length - 1)
                Yield characters(i) & " " & nxt
            Next
        End If
    Next
End Function

If you're using an older version and if you're fine with a non-lazy variant:

Public Shared Function LexicographNonLazy(characters As List(Of String), length As Integer) As IEnumerable(Of String)
    Dim result = new List(Of String)
    For i = 0 To characters.Count - 1
        If length = 1 Then
            result.Add(characters(i))
        Else
            For Each nxt In Lexicograph(characters.GetRange(i + 1, characters.Count - (i + 1)), length - 1)
                result.Add(characters(i) & " " & nxt)
            Next
        End If
    Next
    Return result
End Function
sloth
  • 99,095
  • 21
  • 171
  • 219
0

Try this:

Public Shared Iterator Function Lexicograph(ByVal characters As List(Of String), ByVal length As Integer) As IEnumerable(Of String)
        For i As Integer = 0 To characters.Count - 1
            If length = 1 Then
                Yield characters(i)

            Else
                For Each nxt As String In Lexicograph(characters.GetRange(i + 1, characters.Count - (i + 1)), length - 1)
                    Yield characters(i) & " " & nxt
                Next nxt
            End If
        Next i
 End Function
coder
  • 13,002
  • 31
  • 112
  • 214
-3

Try this Code.. Think so it may solve..

EDIT

  Public Shared Function Lexicograph(characters As List(Of String), length As Integer) As IEnumerable(Of String)
  For i As Integer = 0 To characters.Count - 1
    If length = 1 Then
        yield Return characters(i)
    Else

        For Each nxt As String In Lexicograph(characters.GetRange(i + 1, characters.Count - (i + 1)), length - 1)
            yield Return characters(i) + " " & nxt
        Next
    End If
Next
End Function

Converted by using: Code Translator C# to VB

Max
  • 12,622
  • 16
  • 73
  • 101
coolprarun
  • 1,153
  • 2
  • 15
  • 22
  • 1
    I doubt that would compile - why would VB.Net suddenly revert to lower case keywords for `yield`? – Daniel Kelley Aug 22 '13 at 09:33
  • This code obviously does not work. `If (length = 1) Then yield End If`? `yield` what? Nothing? – sloth Aug 22 '13 at 09:45
  • 1
    You can only use `Yield` in a method marked with `Iterator`. Also, `yield Return characters(i)` should simply be `Yield characters(i)`. You just copy/pasted the wrong result of an online converter. – sloth Aug 22 '13 at 09:50
  • The solution is to Convert the C# coding "C# to VB.NET Code Conversion". we just converted the solution to VB.NET. – coolprarun Aug 22 '13 at 09:53