简体   繁体   English

无法输出数组

[英]Having trouble to output arrays

I am asked to output two arrays in one label my code is as follow: 我被要求在一个标签中输出两个数组,我的代码如下:

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

when I click on the button it doesn't display all the values of the array it displays only the first one of each . 当我单击按钮时,它不会显示数组的所有值,而是仅显示每个数组的第一个。 I am also trying to display it in a label kind of table view. 我也试图以标签形式的表格视图显示它。

You should probably be using List(Of String) instead of arrays, but with that being said, your String.Join is trying to merge two arrays, and it doesn't quite work like that. 您可能应该使用List(Of String)而不是数组,但话虽这么说,您的String.Join试图合并两个数组,但它并非完全如此。

Try adding your strings to a List(Of String) instead, then convert the entire output back into a single array: 尝试将字符串添加到List(Of String) ,然后将整个输出转换回单个数组:

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

If your second array is an array of integers, you would have to convert them to an array of strings: 如果第二个数组是整数数组,则必须将它们转换为字符串数组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM