If you read my previous post you'd know I was involved in some C#->VB COM developing. Well actually VB->C# to be 100% correct.
Anyhoo, in VB you can define a string with a length eg
Dim suckyVBString as String * 9
which defines a string of max length 9 characters. When this string is part of a user defined VB type (or "class") then when the type/class is newed then suckyVBString will be " " rather than "".
So a little unexpectantly doing a String.IsNullOrEmpty(suckyVBStringFromCOM) returns false
a little browse of the IL shows that IsNullOrEmpty contains this code (not exact code I am blogging on my mac but you get the point):
public bool IsNullOrEmpty(string s)
{
if (s != null && s.Length > 0)
  return false;
else
  return true;
}
so while the Marshaled VB string is full of 9 nulls (char /0), it is neither null or empty according to the code.
A quick visit to String.Trim() sorts all this out, but it's a trap for young players if you're not careful.
No comments:
Post a Comment