2.4. Strings

As in VB 6, VB 2005 String types are used to represent text and are a good example of a reference type, as you saw in "Variables," earlier in this chapter. Strings in .NET are immutable, which means that once you've assigned a value to a string variable, it cannot be changed. If the value of a string variable is changed, another string object is created during runtime. Consider this example:

 Dim st As String st = "Hello" st &= " World!" MsgBox(st) ' prints "Hello World!" 

In the above example, two string objects are involved: one for the initialization and one for the concatenation. This problem gets worse if you are doing concatenation in a loop, like the following:

 Dim i As Integer, str As String = "" For i = 0 To 10000        str &= i.ToString     Next 

A much more efficient way to manipulate strings is to use the StringBuilder class, located in the System.Text namespace:

 Dim i As Integer, str As New _        System.Text.StringBuilder()     For i = 0 To 10000        str.Append(i.ToString)     Next 

The "_" (underscore) character is the continuation character in Visual Basic (all versions). It is used to break a long statement into multiple lines.

Visual Basic 2005 Jumpstart 2005
Visual Basic 2005 Jumpstart
ISBN: 059610071X
EAN: 2147483647
Year: 2005
Pages: 86
Authors: Wei-Meng Lee

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net