Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Use TextBox.AppendText instead of concatenating strings
From Wiki
Run the following code in a windowsformsapplication.
Make a form and add three textboxes and 3 labels to it.
then add this code in the code window
- Imports System.Text
- Public Class Form1
- Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- Dim str As New StringBuilder()
- Dim begin As DateTime = Now
- For i As Integer = 0 To 100000
- str.Append("a")
- str.Append(i.ToString)
- Next
- TextBox1.Text = str.ToString
- Label1.Text = "Code took : " & (Now - begin).Milliseconds & " milliseconds"
- Dim str1 As String = ""
- begin = Now
- For i As Integer = 0 To 100000
- str1 &= "a" & i.ToString
- Next
- TextBox2.Text = str1
- Label2.Text = "Code took : " & (Now - begin).Milliseconds & " milliseconds"
- begin = Now
- For i As Integer = 0 To 100000
- TextBox3.AppendText("a")
- TextBox3.AppendText(i.ToString())
- Next
- TextBox3.Text = str1
- Label3.Text = "Code took : " & (Now - begin).Milliseconds & " milliseconds"
- End Sub
- End Class
This is the result.
And allthough the stringbuilder code is still faster the appendtext code is also faster then just concatenation and uses less code. So go for that as much as you can.
Disclaimer: This code was only tested in Visual Studio 2008.




LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.