Is there any difference between vbNullString and ""?

Solution 1:

vbNullString and "" are different. Here is a webpage exerpt that describes the memory usage differences.


"This is the usual way to clear a string variable.

Text$ = ""

What a waste! First of all, the string "" takes 6 bytes of RAM each time you use it. Consider the alternative:

Text$ = vbNullString

So what is this? vbNullString is a special VB constant that denotes a null string. The "" literal is an empty string. There's an important difference. An empty string is a real string. A null string is not. It is just a zero. If you know the C language, vbNullString is the equivalent of NULL.

For most purposes, vbNullString is equivalent to "" in VB. The only practical difference is that vbNullString is faster to assign and process and it takes less memory.

If you call some non-VB API or component, test the calls with vbNullString before distributing your application. The function you're calling might not check for a NULL string, in which case it might crash. Non-VB functions should check for NULL before processing a string parameter. With bad luck, the particular function you're calling does not do that. In this case, use "". Usually APIs do support vbNullString and they can even perform better with it!"


The rest of the article has additional information on optimizing strings that may be insightful as well.

Full webpage: http://www.aivosto.com/vbtips/stringopt.html

Solution 2:

This comparison states that assigning "" to a variable uses 6 bytes of memory whereas using vbNullString will not use any memory.

Personally, I prefer to evaluate the length of a string. If length is 0, then we also arrive at the conclusion that the string is a vbNullString or "". This method is accepted as being the quickest method of checking for a vbNullString.

If Len(string) = 0 Then

You can read a Len vs vbNullString vs "" comparison here.