无法输出数组

时间:2015-11-24 17:46:16

标签: arrays vb.net winforms

我被要求在一个标签中输出两个数组,我的代码如下:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click    
  If block = True And counter <= 23 Then
    Try
      array1(counter) = TextBox1.Text
      Dim txtbox2 As String = CInt(TextBox2.Text)
      array2(counter) = txtbox2
      counter += 1
    Catch ex As Exception
      TextBox2.Text = ""
    End Try
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  Dim arr1 As String
  Dim arr2 As String
  Dim result As String
  For Each values In array1
    arr1 = values
  Next

  For Each values In array2
    arr2 = values.ToString
  Next
  Label3.Text = String.Join("     ", arr1, arr2)
End Sub

当我点击按钮时,它不显示数组的所有值,它只显示每个值中的第一个。我也试图在标签类表视图中显示它。

1 个答案:

答案 0 :(得分:0)

你可能应该使用List(Of String)而不是数组,但话说回来,你的String.Join正在尝试合并两个数组,而且它并不像那样工作。

尝试将字符串添加到List(Of String),然后将整个输出转换回单个数组:

Dim results As New List(Of String)(array1)
results.AddRange(array2)
Label3.Text = String.Join(" ", results.ToArray)

如果你的第二个数组是一个整数数组,你必须将它们转换为一个字符串数组:

Dim results As New List(Of String)(array1)
results.AddRange(array2.Select(Function(x) x.ToString))
Label3.Text = String.Join(" ", results.ToArray)