Solved

Literal "" versus vbNullString

Posted on 2001-06-03
7
1,447 Views
Last Modified: 2010-05-18
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?

0
Comment
Question by:chnelson
7 Comments
 
LVL 15

Expert Comment

by:robbert
ID: 6150612
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 6150613
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
 
LVL 15

Expert Comment

by:robbert
ID: 6150617
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:ameba
ID: 6150889
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
 
LVL 1

Accepted Solution

by:
morgan_peat earned 50 total points
ID: 6151837
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
 
LVL 22

Expert Comment

by:rspahitz
ID: 6153557
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
 
LVL 1

Author Comment

by:chnelson
ID: 6154653
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now