Reputation: 25
So I'm trying to add items into a array and then return all items in the array in a textbox. Can someone tell me what I'm doing wrong? When I click the button, it errors out or I get system.string[]
or it doesn't display all the items, just one full or partial item. edit1: here is the updated code.
Public Class Form1
Dim x As Integer = 0
Dim strencode As String
Dim strletters As String
Dim strholder(0 To 999) As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
strletters = TextBox1.Text
Label2.Text = x.ToString
If TextBox1.TextLength < x Then
x = 0
End If
If TextBox1.TextLength <> 0 Then
If TextBox1.TextLength < x Then
x = 0
End If
Do Until x = TextBox1.TextLength
If TextBox1.TextLength < x Then
x = 0
End If
If TextBox1.TextLength <> 0 Then
If strletters.Substring(x, 1) = "A" Then
strholder(x) = "346,"
x = x + 1
ElseIf strletters.Substring(x, 1) = "B" Then
strholder(x) = "917,"
x = x + 1
End If
End If
Loop
pause:
Dim i As Integer
For i = 0 To (x - 1)
TextBox2.Text = (strholder(i))
Next
End If
End Sub
End Class
Upvotes: 2
Views: 857
Reputation: 11
Your Do Until
block will continue indefinitely if you have any letter different from 'A' and 'B' in your strletters
string. Be sure to always increment the x value:
Do Until x = TextBox1.TextLength
' Some conditions
' Some more conditions
x = x + 1 ' Increment regardless
Loop
Additionally, you can easily concatenate the output of your strholder
array by using String.Join
:
TextBox2.Text = String.Join(String.Empty, strholder)
Update:
Here's a code snippet with a couple of other methods...
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Converting a string to a char array
Dim charArray() As Char = TextBox1.Text.ToCharArray
' Converting a string to a string array
Dim stringArray() As String = TextBox1.Text.Select(Function(c) c.ToString).ToArray
' Concatenate using a For Each loop
For Each ch In charArray ' or stringArray
' do something with each ch...
TextBox2.Text &= ch ' Concatenate using the & operator
'TextBox2.Text += ch ' Concatenate using the + operator
Next
' Converting a char array back to a string
TextBox2.Text = String.Join(String.Empty, charArray)
' Converting a string array back to a string, method 1
TextBox2.Text = String.Join(String.Empty, stringArray)
' Converting a string array back to a string, method 2
TextBox2.Text = stringArray.Aggregate(Function(final, sCh) final & sCh)
' In your case you could just simply...
TextBox2.Text = TextBox1.Text.Replace("A"c, "346,").Replace("B"c, "917,")
End Sub
End Class
As for choosing a concatenation operator, you can take a look at this
Upvotes: 1