I was amused to find myself reading a posting analysing some of my code, which I’d followed because the title intrigued me – Strings Can Be Nil, Even If They Can’t Be Set To Nil
When Charles says he is surprised that some of my code works and Mars chips in and says he thinks it contains a redundant check, I get a little concerned. The port of rbrw-core was a lot of work converting thousands of lines of C++ so I may well have carried some C++ idioms across without thinking.
However, I am aware that a lot of the rbrw code uses variants and I may have written that test after a nasty experience (yes, it needs a comment, if so).
To assuage my curiousity and conscience, I flung together a quick check for variants and nil strings, available on Google Code.
The important bit is invoking the string with a variant, which does indeed appear to pass a nil string to a method, as seen below. Based on Charles’ comment, I’m leaving the verdict open – if indeed the string comparison of is nil is also fired by an empty string, we can’t tell what is happening unless writing plugins to explicitly return nil strings.
dim v as Variant TestString v Sub TestString(foo as String) if foo is nil then print "Yes, Strings can be nil, if they come from an empty Variant" end if End Sub