Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

The type of an empty string

First, let me say that I'm new to .net and I'd be very surprised if I'm not missing something fundamental here. I've been writing a program which works with a database (SQL Server 7, if it matters), and I've got some code which uses IsDBNull. That code fails intermittently if the value of the column being tested is the empty string. I boiled it down to this simple programm:

imports System
imports Microsoft.VisualBasic

class test
    public shared sub main
        try
            dim s as string = ""
            Console.WriteLine(IsDBNull(s))
        catch e as Exception
            Console.WriteLine(e.Message())
        end try
    end sub
end class


When I ran this program on my computer just now, it prints "False" as one would expect.  Yesterday, it threw an exception. Unfortunately I didn't write the message down, but it had something to do with an invalid cast. So, how is an empty string different from any other string?

public shared sub main
    try
        dim s = ""
        Console.WriteLine("TypeCode.String = " & TypeCode.String)
        Console.WriteLine("TypeCode.Empty = " & TypeCode.Empty)
        Console.WriteLine("Type of s: " & GetTypeCode(s))
        Console.WriteLine("Type of nothing: " & GetTypeCode(nothing))
        Console.WriteLine("s equals nothing: " & (nothing = s))
    catch e as Exception
        Console.WriteLine(e.Message())
    end try
end sub

Today, on my computer, that program produces this output:

TypeCode.String = 18
TypeCode.Empty = 0
Type of s: 18
Type of nothing: 0
s equals nothing: True


The fact that s = nothing is true would seem to imply that empty strings are represented by a null reference rather than a reference to, well, an empty string. On the other hand, the value of GetTypeCode(s) seems to imply that an empty string is in fact a string. Either way, there seems to be some sleight of hand going on.

Questions:

1. Are empty strings represented by null references?
2. If so, why is the type of an empty string still String?
3. Why does IsDBNull("") sometimes fail?

Confused
Friday, June 18, 2004

Empty strings are definitely different from null references (Nothing) in .NET. I have no idea why VB.NET would say they have the same type code.

Chris Nahr
Friday, June 18, 2004

Sorry, I see they actually don't have the same type code but VB says that an empty string equals nothing. Anyway, this shouldn't be true either.

Chris Nahr
Friday, June 18, 2004

Some Databases, Oracle f.ex  treats '' as NULL.  Perhaps VB.NET is trying to behave thusly?

Koz
Saturday, June 19, 2004

The proper way to test whether a variable is nothing is to use the Is operator:

Console.Writeline(s is nothing)
Console.Writeline(s = nothing)

This will print

False
True

The first line tells you that s, in fact, is not nothing.

Why the second result? See the VB Language Specification section 2.4.7: "Nothing is a special literal; it does not have a type and is convertible to all types in the type system. When converted to a particular type, it is the equivalent of the default value of that type."

Mike Gunderloy
Saturday, June 19, 2004

Thanks Mike. That explains it.

No longer confused
Wednesday, June 23, 2004

Wait a moment... so the empty string is a string's default value in Visual Basic? That's different from C#, too, and different from the CLI initialization of the System.String datatype.

Chris Nahr
Friday, June 25, 2004

In the technical nit-picking sense, the "default value" for string doesn't exist, because string isn't a value type. Moreover, I cannot write would I would consider the de-facto test of a type's "default value":

string s = new string();

because string doesn't have a parameterless constructor. It's worth noting that this does work for all value types:

int i = new int();

and i will be zero (the default value for int). It's also worth noting that, in C#, you cannot use an uninitialized value type. If you write:

int i;
Console.WriteLine("{0}", i);

you'll get compiler error CS0165.

I'm guessing that the notion of the empty string as the "default value" for string arises from the Convert class. When you pass null/nothing to Convert.ToString(), it returns back an empty string. This is probably the implicit conversion happening under the covers for VB.NET when you write:

Console.WriteLine(s = nothing)

VB.NET sees a string on the left hand side, and converts the value on the right hand side to a string implicitly via Convert.ToString(). At least, that's my best guess, as I'm not a VB.NET user.

Brad Wilson (dotnetguy.techieswithcats.com)
Sunday, June 27, 2004

*  Recent Topics

*  Fog Creek Home