Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which is better "" or Empty 3

Status
Not open for further replies.

Brawn

Programmer
Jul 5, 2001
73
0
0
CA

Which is better to use:

Code:
string = ""
or
Code:
string = Empty

Pros?
Cons?
[smile] Brawn

"My mind is my Shrine,
and my body the Temple around it."

-The difference between genius and stupidity is that genius has its limits-
 
From a practical standpoint I don't think it matters.

But since Empty is a variant subtype to indicate an unitialized variable, and technically, setting a string to "" is an initialization

I prefer ""
Good Luck

 
Actually setting a variable dimmed as a string to "Empty" will hold "" as its value. Only a variant can actually hold the value "Empty", the compiler will convert the Empty to a "" so there is actually no difference in the value that the string holds.

As a matter of design (and opinion) I'd stay away from the use of "Empty" if possible.
 
Of course 'string = Empty' does have the dubious advantage that it might be considered self-documenting...
 
Hi,
Aivosto's VBAnalyzer says to use vbNullString.That it is a special constant, available in VB4 and later,that saves memory and speeds up the assignment.It goes on to say to use LenB() to check for zero-length strings it's faster than:
If string = "" Then

e.g.
string = vbNullString
If LenB(string) = 0 Then
MsgBox "Empty String"
End If

Jon
 
Jon,
I just set up a couple of test loops and your info appears to be correct. The string=vbNullString is about 5 times faster than string="" on my computer using VB6 and the LenB(string)=0 test is a bit faster vs string="" (about 700 vs 850 on my test loop). As slow as my applications are, I think I might take my next break replacing all the "" 's.
Bob
 
That's great,

I didn't even know there was an empty until I
declared (or tried ) to declare a const "" as Empty

here's a couple of stars.
[smile]
Thanks alot Brawn

"My mind is my Shrine,
and my body the Temple around it."

-The difference between genius and stupidity is that genius has its limits-
 
String = vbNullString is much faster than using "" (200-300%)

vbNullString is a constant and doesn't need to be interpeted but once.
"" will be interpeted every time

Using String= Empty is use wrong here - this is used for testing to determine if a variable has ever been initialized. Still if used, it is slow because of the extra testing.

I use vbNullString only - it is also much easier to read.
 
I would be carefull when dealing with recordset's that are not nullable though. I keep variables empty but when writting to a DB, I use the standard of

rs.Fileds(1) = strString & ""

Craig, mailto:sander@cogeco.ca

Remember not to name the Lambs...
It only makes the chops harder to swallow
 
Good note Craig,
So on the recordset's

rs.field(1) = str & ""
and
rs.field(1) = str & vbNullString

will not produce desirable results?

MSDN documents:
vbNullString
- String having value 0
- Not the same as a zero-length string (""); used for calling external procedures. Brawn

"My mind is my Shrine,
and my body the Temple around it."

-The difference between genius and stupidity is that genius has its limits-
 
CraigSander: Assigning a rs a variable of type String with-out a value assigned explicit to it is the same as assigning "" or vbNullString. (By the way, vbNullString is a constant defined as "" and nothing more, or less, than using "" , except for the fact, as mentioned above, that it is more readable and faster). So assigning a recordset with a string variable and adding "" to it it an extra step not needed. It would be the same as writing: rs.Fileds(1) = "" & "". A String variable is never Null - it always holds at least a zero length string. A variant on the other hand is different. If you use variables defined as a variant, then you will need to decide if you want to assign a Null or an string of zero length to the recordset.
A text field is a table is of course not a string. Leaving the field as Null has its' advantages. And a good program technique is to ALWAYS handle fields with Null values.




 
vbNullString is not the same as ""

vbNullString is a genuine null pointer. An empty string such as "" is a pointer to a string (actually a BSTR) whose length is 0 (and has a trailing null). In most cases for VB programmers this difference is not overly important, although in the speed issues mentioned above vbNullString faster because it avoids the creation and deletion of temporary BSTRs that "" involves.

Without going into the detail of how these things work, the following simple code illustrates the difference between the two:

Dim s As String
Dim t As String
s = vbNullString
t = ""

Debug.Print StrPtr(s) ' What BSTR is 's' pointing to?
Debug.Print StrPtr(t) ' What BSTR is 't' pointing to?
 
I am trying to figure out from above who said vbNullString and "" are the same.

The results of setting the value of a field in a database table to a vbNullString or "" in addition to setting the value of a rs field to a String variable with zero length, and then updating that field in the database table are still the same. Doing it twice is not needed.

That was my point above.

 
CCLINT: vbNullString is a constant defined as ""

I wasn't, however, necessarily disagreeing with the rest of the point you were making.
 
Ok.

In the object browser:
vbNullString is a constant defined as "" :

Const vbNullString = ""

-That still isn't saying that it is the same as a string variable with zero length - no one said that.

VbNullChar is also a constant defined as "" - but in this case that is the same as a zero length string variable.







 
Sorry but vbNullChar is not the same as a zero length string variable - it is the same as the Chr$(0) - therefore it will have a length of 1.
 
The object browser is misleading; to all intents and purposes within VB vbNullString almost always acts as if it were indeed a constant defined as "" (because VB itself does a bunch of work behind the scenes to hide what is really going on for you). As I said to start with, in most cases for VB programmers this difference is not overly important

The moment you leave the the safety of VB however all bets are off, which is why it important to know the difference. The following code illustrates the point:
[tt]
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Command1_Click()
Dim hWndUsingQuotes As Long
Dim hWndUsingvbNullString As Long

MsgBox "Now searching for any top-level window, don't care about title or class"

hWndUsingQuotes = FindWindow("", "")
MsgBox "hwndUsingQuotes = " & hWndUsingQuotes & " - oops, failed"

' This should get exactly the same result if vbNullString really is just a constant
' defined as ""
hWndUsingvbNullString = FindWindow(vbNullString, vbNullString)
MsgBox "hwndUsingvbNullString = " & hWndUsingvbNullString & " - aha! Found something"

End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top