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.
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,
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
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
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 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.
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"
' 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"
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.