Question Need help with the stringbuilder class

bchekuri

New member
Joined
May 16, 2012
Messages
1
Programming Experience
Beginner
Hi,

I need help understanding the usage of the stringbuilder class. The scenario is: i have a form set up with 2 richtextbox controls. I would like to paste lets say 40000 values in richtextbox 1 and click a button that adds a predefined string to these values and they would finally display in richtextbox2. But my code below doesn't really seem to work. It takes about an hour to perform this operation. Please help!

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

Dim x As Long
Dim Mystring As New System.Text.StringBuilder()


For x = RichTextBox1.Lines.GetLowerBound(0) To RichTextBox1.Lines.GetUpperBound(0)

array1(x) = Mystring.Append("INSERT INTO #TMP (TMP) Values ('")
array1(x) = Mystring.Append(RichTextBox1.Lines(x))
array1(x) = Mystring.Append("')")

RichTextBox2.Text = array1(x).ToString() & vbCrLf


Next

End Sub
End Class

Thanks,
Bharath
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,397
Location
Sydney, Australia
Programming Experience
10+
You have all sorts of problems there I'm afraid.

1. Do not EVER use the Lines property of a TextBox or RichTextBox over and over like that. You shouldn't do that with any property but especially not with Lines. If you want to use a property value multiple times then you declare a variable, get the property value once and once only and assign it to the variable, then use that variable over and over. That is more efficient for any property but particularly for lines. That because Lines doesn't return the same array every time. Each time you get the property it actually creates a new array. That means that your code is going to create 40,002 arrays that all contain the same data when you could simply create one at the start and use it over and over.

2. I'm not sure what that 'array1' is for but it doesn't appear to be doing anything useful so get rid of it. You are creating an array with 40,000 elements where each element is exactly the same StringBuilder that you already have.

3. Here's the big one. What's the point of the StringBuilder in the first place? To allow you to build the String piece by piece and then use it in its entirety when you're done, right? So why then are you using it inside the loop when you know that you're not done building it? You are setting the Text of the second RichTextBox inside the loop, which means that you are changing it 40,000 times. What use is that when you only want the last value? You should be setting the Text once and once only, when you have finished building the String.

4. I would also tend to use AppendFormat once rather than Append three times.

Having said all that, you don't actually need a StringBuilder at all. If your intention is to learn how to use the StringBuilder then by all means proceed but it's not required. I would tend to get the Lines of the first RTB and then loop through that array and simply modify the value of each element. That won't affect the original RTB because the Lines property is not "live". Once you've edited every element you can simply assign the array to the Lines property of the second RTB.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,516
Location
Norway
Programming Experience
10+
I would like to paste lets say 40000 values in richtextbox 1 and click a button that adds a predefined string to these values and they would finally display in richtextbox2.
40000 lines of "INSERT INTO #TMP (TMP) Values ('" in textbox? If you don't actually need to see those lines there you should avoid that GUI element altogether, and instead take the data from clipboard, manipulate it and output it where appropriate, for example back to clipboard or to a file or something.
 
Top Bottom