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.
|
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 3, lets you use the For Each construct to enumerate a String as a collection of Chars.
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"
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.
While Strings are considered to be reference types, the String objects themselves are immutable. They cannot 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 object is then cleaned up by the garbage collector.
Top |