Friday, December 23, 2005

Strange Bug in VB 2005

That's THE strangest bug I ever encountered while working with Visual Basic. Ever!

I was parsing a list of strings that was separated by a CR/LF pair. Something like this:
The quick brown fox jumped over the lazy dog.[CR][LF]
Miss Piggy sent Kermit an email.[CR][LF]
Lois was cheating on Clark[CR][LF]
So, I used the following command to get an array of strings:
Dim aStrList as String() = _
strFullList.Replace(vbLf, "").Split(vbCr)
Simple, right?

And then I went to my loop:
For Each strItem As String In aStrList
If (strItem = "") Then
Exit For
End If

'... do whatever...
Next
The first time I ran it it crashed my code. I took me some time to discover why.

Apparently the last item of the array, instead of being and empty string, is a String which contains a single "character" Nothing.

The ONLY way I could find to workaround this stuff was changing my test line to look like this:
If (strItem.Chars(0) = Nothing) Then
For the sake of completion, this strange string has the following properties:

TestResult
strItem = ""False
strItem = NothingFalse
strItem.Length = 0False
strItem.Length = 1True

However when examined in the "Quick Watch" window it does show strItem as an empty string.

0 Comments:

Post a Comment