-3

再会,

我在将函数从 C# 转换为 VB.NET 时遇到了一点问题。这是代码:

    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;
        }
    }

我知道有一些在线转换器,但由于编译器说我的代码中有错误/警告(从 String 到 IEnumerable 的转换),所以这些转换器没有正确工作。

我对列表和 IEnumerable 接口不太熟悉,这就是我来这里的原因。

如果有人可以为此或任何其他类型的提示找到解决方案,我们将不胜感激。

提前致谢。

4

3 回答 3

1

如果您使用的是 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

如果您使用的是旧版本并且可以使用非惰性变体:

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
于 2013-08-22T09:44:07.747 回答
0

试试这个:

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
于 2013-08-22T09:51:50.690 回答
-3

试试这个代码..想想它可能会解决..

编辑

  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

通过使用转换:代码翻译器 C# 到 VB

于 2013-08-22T09:28:17.073 回答