| You are here: | About>Computing & Technology>Visual Basic |
![]() | Visual Basic |
StringBuilder ... A New Object in .NETOct 27 2007 It's a lot faster than Strings!Strings are one of the most familiar, and useful, types in Visual Basic. Your average programmer simply thinks of them as a chunk of text ... Dim myString as String = "This is a chunk of text!" In fact, however, a String in VB.NET is more like an array of Chars (characters) than a single object. That's why you can treat it like an array as shown in the following code: Private Sub DisplayAcronym_Click( _ The result is shown in the illustration: -------- The Microsoft documentation describes the String object as "immutable". That means that it can't be changed. But we know that you can change a String easily in code: myString = "A new chunk of text." What actually happens is that VB.NET ...
That's a lot of processing! One advantage this gives you is that a String can, in most practical situations, have unlimited length. The technical limitation stated by Microsoft is "0 to approximately 2 billion (2 ^ 31) Unicode characters." But the performance hit you take using them is usually a bigger consideration. To solve the performance problem, VB.NET gives you a completely new object called StringBuilder. To be quite clear, a StringBuilder object is not a String. In fact, you will have to convert it to a String to use it in many situations, usually using the ToString method that all objects have. A Stringbuilder object also has a completely new set of methods and properties. So if you are going to use it, you have to become familiar with them. The major new methods are:
StringBuilder, like Strings, will automatically be reallocated by the compiled code if you try to save too much text in it. But it doesn't happen until the Capacity of the StringBuilder is actually exceeded and you can control the process with more new methods and properties. This code ... Dim TestAppend As New System.Text.StringBuilder ... tells me that the initial capacity of a StringBuilder is 16 chars. If I Append a String that is 17 chars long and then check ... Dim TestAppend As New System.Text.StringBuilder ... I find that the new Capacity is 32. Microsoft states that the Capacity doubles when the old limit is reached. But wait! There's more! You can also control the Capacity. You can set the starting Capacity and you can control the maximum Capacity. If you allocate the StringBuilder this way ... Dim TestAppend As New System.Text.StringBuilder(10, 20) ... then it will start out with the capability of storing strings of length 10, but the statement will throw an ArgumentOutOfRangeException if you try to store a string or set the Capacity to more than 20. On the next page, we find out just how much faster StringBuilder really is! |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


