Solved

Literal "" versus vbNullString

Posted on 2001-06-03
7
1,441 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 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

16 Experts available now in Live!

Get 1:1 Help Now