Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between vbNullString and ""?

Tags:

In VBA, will it make any difference if I compare a string, or similar, against vbNullString instead of against an empty string; ""? If so what differences are there between the two?

like image 964
eirikdaude Avatar asked Sep 07 '15 09:09

eirikdaude


1 Answers

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

like image 112
GoeschWho Avatar answered Sep 23 '22 03:09

GoeschWho