[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1484
  • Last Modified:

Literal "" versus vbNullString

Is it true that vbNullString is better than literal "" while:

txtCustName.Text = ""

Use vbNullString is a special constant, available in VB4 later, that saves memory and speeds up the assignment.

If the statement is a comparison, use LenB() instead of literal "" like:
If LenB(txtCustName.Text) = 0 Then

I am using a piece of software named: Project Analyzer
and i want to know that whether it is true.

If it is true, i will take an action to adopt the above standards in our R&D team. As now it is still in an earily stage and nothing gonna to be mese yet.

Any comments or suggestions?

1 Solution
Yes, it's true (but don't ask me why); performance gain ~50%:

VBA Code Optimisation

Btw, you need to close all of your questions, not just every second (or whatever criteria you apply).
I thought vbNullString was: (quoting the object browser) "Constant for  use when calling external procedures requiring a string whose value is zero"

In other words, a constant required for calling an API that needs a null string pointer.  

Bruce McKinney suggests making an empty string constant since none really exists in VB.

Public Const gsEmpty = ""

Otherwise the string table in your executable is littered with each declaration of an empty string literal.
From the above article:
Assigning vbNullString, "VBA doesn't have to create a new string each time you make the assignment, but uses its own internal pointer to an empty string, saving a substantial amount of work each time you initialise a string variable".
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

vbNullString constant (or any constant) is faster than variable, I use it when code must be optimized for speed.

But, typing vbNullString is slow - for "" you can press only one key twice.

"" will save time, also your keyboards will last longer - this means less polution.
If you print your code you'll need more ink for vbNullString... :-)
A String in VB is actually a pointer to an array in memory (the array being Unicode values containing your string).

The address in memory pointed to by this variable points to the start of the character array.  At the end of this array there is a 2-byte null terminator.  There are also 4 bytes preceeding this address, which contain the length of the string in bytes.

If you use "" to declare an empty string, you receive a pointer to an address, where the size of the character array is 0.  This still contains the 4-byte length field, and the 2-byte terminator.

If you use vbNullString, you still receive a pointer, but that pointer does not point to any character array (it is null), so there is no 4-byte length field, and no 2-byte terminator.

Therefore, it's better on memory and performance to use vbNullString, rather than "".

For more information, check out MSDN.  Partial books section, WIN32 API programming with Visual Basic, Chapter 6 (Strings).
Many of the above explanations were correct.  Here's my 2-cents...

Constants are generally faster than expressions for various reasons:

1) They are pre-calculated, and "hard-coded" into the compiled code.
2) They do not require the processor to evaluate the expression, thereby reducing processing.
3) If the constant is a string, the string space is preallocated and defined as a fixed string rather than a variable string.

So given the above, vbNullString is quicker than "".

Should you use it?
In most cases, I think it would be practical, but the performance gains are minimal in most cases, so it does not necessarily have a major impact if you don't.
chnelsonAuthor Commented:
Thanks very much all gentlemen!

ameba, your point of view is really wonderful. My colleage said the points should be transferred to you. Ha...

However, i found the last 2 comments are the best for me. i would use vbNullString later in my programming.

Robbert, the article you posted is really helpful! Thanks thanks a lot!

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now