简体   繁体   中英

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.

Try adding your strings to a List(Of String) instead, then convert the entire output back into a single array:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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