String.Together Snazzy String Routines (Continued)
If the array is an object array, then the ToString method of each object is used to furnish the raw material for concatenation. This is a great shortcut for getting an assembled string on an array of strings or objects, because you don't have to write looping logic to go through the array.
Let's finish up with an aspect of strings in .NET that occasionally trips up traditional VB programmers. There is a difference in how strings are initialized between the two environments. In VB6, it's simple: Declaring a string in VB6 gives it a value immediately, that of an empty string. You could compare it against either the vbNullString constant or against a literal empty string (""), and it would match.
In most routine cases, .NET versions of Visual Basic act the same way. For example, vbNullString is replaced by String.Empty, which is a stand-in for a zero-length string. You can compare a new string that hasn't been initialized to either String.Empty or a zero-length string (""), and the result will return True:
Dim s As String
If s = String.Empty Then
'The above condition evaluates to True
End If
But that similarity obscures some underlying differences. When a .NET string is initialized, it's not really a zero-length string. Instead, it's a null object reference, which means it evaluates to Nothing in VB.NET. You can see the difference by running this code:
Dim s1 As String
Dim s2 As String = String.Empty
If s1 Is Nothing Then
Console.WriteLine("s1 is Nothing")
End If
If s2 Is Nothing Then
Console.WriteLine("s2 is Nothing")
End If
The first If statement's conditional ("s1 Is Nothing") evaluates to True, but the second If statement's condition ("s2 Is Nothing") doesn't.
On the other hand, the difference disappears when a comparison is made to an empty string. Consider this code:
Dim s1 As String
Dim s2 As String = String.Empty
If s1 = String.Empty Then
Console.WriteLine("s1 is Empty")
End If
If s2 = String.Empty Then
Console.WriteLine("s2 is Empty")
End If
In this case, both conditionals evaluate to True. How can that be? How can s1 be equivalent to both Nothing and String.Empty, even though Nothing and String.Empty are not themselves equal?
The answer is that the VB compiler is trying to keep you out of trouble. The conditional "s1 = String.Empty" is compiled as "s1 Is Nothing OrElse s1 = String.Empty". So the conditional is True for an uninitialized string.
This works reasonably well because you rarely need to care if a string is uninitialized vs. simply empty. However, if you want to be more explicit in your checking, and get somewhat more readable code in the bargain, you can use a method of the String class called String.IsNullOrEmpty:
Dim s As String
Dim s2 As String = String.Empty
Dim b As Boolean
b = String.IsNullOrEmpty(s)
' b contains True
b = String.IsNullOrEmpty(s2)
' b still contains True
You can probably file this in the "you'll never need to know this" category, but the difference between an uninitialized string and an empty string can also be seen when using the ToString method:
Dim s1 As String
Dim s2 As String = String.Empty
Console.WriteLine(s2.ToString)
Console.WriteLine(s1.ToString)
If you run this code, the last line generates a null object reference exception. However, you'll rarely have a need to use the ToString method of a String. A String is already a String datatype, so the String.ToString method is usually redundant.
This is my second article on strings in as many months, and there's still more to tell. I highly recommend that you take the time to try out the techniques described in this article, and see what .NET's native string features can do for your applications.
About the Author
Billy Hollis is an author and software developer from Nashville, Tennessee. Billy is co-author of the first book ever published on Visual Basic .NET, VB .NET Programming on the Public Beta. He has written many articles, and is a frequent speaker at conferences. He is the Regional Director of Developer Relations in Nashville for Microsoft, and runs a consulting company focusing on Microsoft .NET. Reach him at billyhollis@gmail.com.
Back to top
|