美好的一天,
将函数从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接口,这就是我来到这里的原因。
如果有人可以为此或任何其他类型的小费找到解决方案,我们将不胜感激。
提前致谢。
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分:-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
转换