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?

LVL 1
chnelsonAsked:
Who is Participating?
 
morgan_peatCommented:
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).
0
 
robbertCommented:
Yes, it's true (but don't ask me why); performance gain ~50%:

VBA Code Optimisation
http://www.microsoft.com/OfficeDev/Articles/movs101.htm

Btw, you need to close all of your questions, not just every second (or whatever criteria you apply).
0
 
PaulHewsCommented:
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
robbertCommented:
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".
0
 
amebaCommented:
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... :-)
0
 
rspahitzCommented:
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.
0
 
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.