[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Literal "" versus vbNullString

Posted on 2001-06-03
Medium Priority
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?

Question by:chnelson
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 15

Expert Comment

ID: 6150612
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).
LVL 38

Expert Comment

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.
LVL 15

Expert Comment

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".
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 15

Expert Comment

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... :-)

Accepted Solution

morgan_peat earned 200 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).
LVL 22

Expert Comment

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.

Author Comment

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!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Suggested Courses

649 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