String Bug & Workaround

  • Thread starter Thread starter MD Cobb
  • Start date Start date
M

MD Cobb

Bug: There's a small bug regarding strings in VB.NET
2003. If you append char's to a string but the first char
appended is nothing then the string value remains nothing
regardless of how many chars are appended thereafter or
their values. This also applies if the first value in a
StringBuilder class is equal to nothing - regardless of
the rest of the content the StringBuilder.ToString will
also return a null string.

Workaround: do not start string building off with chars
that are equal to nothing. :)
 
MD Cobb said:
Bug: There's a small bug regarding strings in VB.NET
2003. If you append char's to a string but the first char
appended is nothing then the string value remains nothing
regardless of how many chars are appended thereafter or
their values. This also applies if the first value in a
StringBuilder class is equal to nothing - regardless of
the rest of the content the StringBuilder.ToString will
also return a null string.

Workaround: do not start string building off with chars
that are equal to nothing. :)

A character is a value type and can not be nothing. Well, the IDE displays
"Nothing" whenever the character value is chr(0), but IMHO this is not
correct.

How do you append Nothing to a stringbuilder? You can not use
sb.Append(Nothing)
because the compiler doesn't know which overloaded version to use. If I
write
dim s as string
dim sb as new stringbuilder
sb.Append(CChar(Nothing))
sb.Append("abc")
s = sb.ToString
msgbox sb.length
the msgbox displays "4". It's just the IDE that cuts the output at chr(0) in
the tooltips, in the locals, watch and other windows.
 
MD Cobb said:
Try This

Dim newChar As Char
Dim str As String

str += newChar

str += "4"

MsgBox(str)

MsgBox(str.Length)

The MsgBox function and the messagebox class is only a wrapper for the API
function MessageBox. Strings are usually terminated by a chr(0) when API
functions are called, that's why the text after chr(0) is not displayed,
but as the length of the string shows, it is not empty. It's length is 2.
 
-----Original Message-----
Bug: There's a small bug regarding strings in VB.NET
2003. If you append char's to a string but the first char
appended is nothing then the string value remains nothing
regardless of how many chars are appended thereafter or
their values. This also applies if the first value in a
StringBuilder class is equal to nothing - regardless of
the rest of the content the StringBuilder.ToString will
also return a null string.

Workaround: do not start string building off with chars
that are equal to nothing. :)
.
 
Thanks, All. However this not just a bug in the IDE:

Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
MsgBox("After Get String Value Is: " & GetString
())
TextBox1.Text = GetString()
End Sub

Public Function GetString() As String
Dim newChar As Char
Dim str As String

str += newChar

str += "4"

MsgBox(str)
MsgBox(str.Length)
Debug.WriteLine(str)

Return str
End Function
 
Back
Top