Section 16.1. Creating Strings

   

16.1 Creating Strings

VB.NET treats strings as if they were built-in types. When you declare a VB.NET String using the String keyword, you are in fact declaring the object to be of the type System.String, one of the built-in types provided by the .NET Framework Class Library.

In .NET, each String object is an immutable sequence of Unicode characters . In other words, methods that appear to change the String actually return a modified copy; the original String remains intact.

The declaration of the System.String class is:

 NotInheritable Public Class String    Implements IComparable, ICloneable, IConvertible, IEnumerable 

This declaration reveals that the class is NotInheritable, meaning that it is not possible to derive from the String class. The class also implements four system interfaces ”IComparable, ICloneable, IConvertible, and IEnumerable ”which dictate functionality that System.String shares with other classes in the .NET Framework.

The IComparable interface is implemented by types that can be sorted. Strings, for example, can be alphabetized; any given string can be compared with another string to determine which should come first in an ordered list. IComparable classes implement the CompareTo( ) method.

ICloneable objects can create new instances with the same value as the original instance. In this case, it is possible to clone a String object to produce a new String object with the same values (characters) as the original. ICloneable classes implement the Clone( ) method.

IConvertible classes provide methods to facilitate conversion to other primitive types; these methods include ToInt32( ), ToDouble( ), and ToDecimal( ).

IEnumerable, discussed in Chapter 15, lets you use the For Each construct to enumerate a String as a collection of Chars.

16.1.1 String Literals

The most common way to create a string is to assign a quoted string of characters, known as a string literal , to a user -defined variable of type String. The following code declares a string called newString that contains the phrase This is a string literal :

 Dim newString As String = "This is a string literal" 

16.1.2 The ToString( ) Method

Another common way to create a string is to call the ToString( ) method on an object and assign the result to a string variable. All the built-in types override this method to simplify the task of converting a value (often a numeric value) to a string representation of that value. In the following example, the ToString( ) method of an Integer type is called to store its value in a string:

 Dim myInteger As Integer = 5 Dim integerString As String = myInteger.ToString( ) 

The call to myInteger.ToString( ) returns a String object, which is then assigned to the string variable, integerString.

16.1.3 Strings Are Immutable

While Strings are considered to be reference types, the String objects themselves are immutable. They can not be changed once created. When you appear to be changing a String, what is actually happening is that a new String is being created and the old String destroyed . Thus, suppose you write,

 Dim myString as String = "Hello" myString = "GoodBye" 

The first line creates a String object on the heap with the characters Hello and assigns a reference to that string to the variable myString . The second line creates a new String object with the characters GoodBye and assigns a reference to that new String to the reference myString. The original String is then cleaned up by the garbage collector.

   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

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