Mark Kramer
Mark Kramer

Reputation: 3214

How to get an array to display all its values at once

Here is some sample code:

Dim arrValue(3) as Integer
arrValue(0) = 5
arrValue(1) = 4
arrValue(2) = 7
arrValue(3) = 1

How can I display those four values next to each other.

More specifically, given those values how can I make txtValue.Text = 5471

Edit:

The idea I had would be to use some sort of function to append each one to the end using a loop like this:

Dim finalValue
For i As Integer = 3 To 0 Step -1
    arrValue(i).appendTo.finalValue
Next

Obviously that code wouldn't work though the premise is sound I don't know the syntax for appending things and I'm sure I wouldn't be able to append an Integer anyway, I would need to convert each individual value to a string first.

Upvotes: 5

Views: 65168

Answers (5)

하정우
하정우

Reputation: 1

for i = lbound(arrValue) to ubound(arrValue)
ss=ss & arrValue(i)
next i
end for 
debug.print ss

Upvotes: 0

kahila kalombo
kahila kalombo

Reputation: 1

Dim value as string = ""


For A As Integer = 1 To Begin.nOfMarks
   value += "Mark " & A & ": " & (Begin.Marks(A)) & vbCrLf 
Next A

Upvotes: -1

Chris Dunaway
Chris Dunaway

Reputation: 11216

Another method is to use String.Join:

Sub Main
    Dim arrValue(3) as Integer
    arrValue(0) = 5
    arrValue(1) = 4
    arrValue(2) = 7
    arrValue(3) = 1

    Dim result As String = String.Join("", arrValue)

    Console.WriteLine(result)
End Sub

Upvotes: 16

Guffa
Guffa

Reputation: 700402

Convert the integers to strings, and concatenate them:

Dim result as String = ""
For Each value as Integer in arrValue
  result += value.ToString()
Next

Note: using += to concatenate strings performs badly if you have many strings. Then you should use a StringBuilder instead:

Dim builder as New StringBuilder()
For Each value as Integer in arrValue
  builder.Append(value)
Next
Dim result as String = builder.ToString()

Upvotes: 1

Justin Self
Justin Self

Reputation: 6265

If I understand your question correctly, you can use StringBuilder to append the values together.

Dim finalValue as StringBuilder
finalValue = new StringBuilder()
For i As Integer = 3 To 0 Step -1
    finalValue.Append(arrValue(i))
Next

Then just return the finalValue.ToString()

Upvotes: 1

Related Questions