Results 1 to 3 of 3

Thread: Need help with the stringbuilder class

  1. #1
    bchekuri is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.0
    Join Date
    May 2012
    Posts
    1
    Reputation
    0

    Need help with the stringbuilder class

    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

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,339
    Reputation
    1543
    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.

  3. #3
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,192
    Reputation
    2368
    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.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking