在VB.Net文本框中调用Sub

在VB.Net文本框中调用Sub,vb.net,Vb.net,我设法将编写的代码转换为C#,我需要一些帮助。我不知道如何让他们在我的文本框中显示适当的值 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' Call (Main) - How to call in a Textbox1.Text End Sub 代码: 当您运行代码时,您将看到它在控制台.WriteLine(“sum”(&String.Join(“,”,pa

我设法将编写的代码转换为C#,我需要一些帮助。我不知道如何让他们在我的文本框中显示适当的值

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' Call (Main) - How to call in a Textbox1.Text
    End Sub
代码:


当您运行代码时,您将看到它在
控制台.WriteLine(“sum”(&String.Join(“,”,part.[Select])(函数(n)n.ToString()).ToArray())和“=”+目标)行中一次输出一组结果

由于您希望一次性获得结果集,因此需要在该点累积结果集,而不是写入控制台,并且Sub需要是一个带有累加器另一个参数的函数

进行这些修改:

Public Class Form1

    ' Derived from the code at https://stackoverflow.com/questions/4632322/finding-all-possible-combinations-of-numbers-to-reach-a-given-sum

    Function SumUpRecursive(numbers As List(Of Integer), target As Integer, part As List(Of Integer), solutions As List(Of List(Of Integer))) As List(Of List(Of Integer))
        Dim s = part.Sum()

        If s = target Then
            ' MsgBox("sum(" & String.Join(",", part.[Select](Function(n) n.ToString()).ToArray()) & ")=" & target)
            solutions.Add(part)
        End If

        If s >= target Then
            Return Nothing
        End If

        For i As Integer = 0 To numbers.Count - 1
            Dim remaining = New List(Of Integer)()
            Dim n As Integer = numbers(i)

            For j As Integer = i + 1 To numbers.Count - 1
                remaining.Add(numbers(j))
            Next

            Dim part_rec = New List(Of Integer)(part)
            part_rec.Add(n)
            SumUpRecursive(remaining, target, part_rec, solutions)

        Next

        Return solutions

    End Function


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim numbers As Integer() = {3, 9, 8, 4, 5, 7, 10}
        Dim target As Integer = 15
        Dim nums = SumUpRecursive((numbers.ToList()), target, New List(Of Integer), New List(Of List(Of Integer)))

        If nums Is Nothing Then
            MsgBox("Failure :(")
        Else
            MsgBox(String.Join(vbCrLf, nums.Select(Function(b) String.Join(", ", b))))
            ' TextBox1.Lines = nums.Select(Function(b) String.Join(", ", b)).ToArray()
        End If

    End Sub

End Class
我相信有一种更整洁的书写方式

Public Class Form1

    ' Derived from the code at https://stackoverflow.com/questions/4632322/finding-all-possible-combinations-of-numbers-to-reach-a-given-sum

    Function SumUpRecursive(numbers As List(Of Integer), target As Integer, part As List(Of Integer), solutions As List(Of List(Of Integer))) As List(Of List(Of Integer))
        Dim s = part.Sum()

        If s = target Then
            ' MsgBox("sum(" & String.Join(",", part.[Select](Function(n) n.ToString()).ToArray()) & ")=" & target)
            solutions.Add(part)
        End If

        If s >= target Then
            Return Nothing
        End If

        For i As Integer = 0 To numbers.Count - 1
            Dim remaining = New List(Of Integer)()
            Dim n As Integer = numbers(i)

            For j As Integer = i + 1 To numbers.Count - 1
                remaining.Add(numbers(j))
            Next

            Dim part_rec = New List(Of Integer)(part)
            part_rec.Add(n)
            SumUpRecursive(remaining, target, part_rec, solutions)

        Next

        Return solutions

    End Function


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim numbers As Integer() = {3, 9, 8, 4, 5, 7, 10}
        Dim target As Integer = 15
        Dim nums = SumUpRecursive((numbers.ToList()), target, New List(Of Integer), New List(Of List(Of Integer)))

        If nums Is Nothing Then
            MsgBox("Failure :(")
        Else
            MsgBox(String.Join(vbCrLf, nums.Select(Function(b) String.Join(", ", b))))
            ' TextBox1.Lines = nums.Select(Function(b) String.Join(", ", b)).ToArray()
        End If

    End Sub

End Class