The Difference Between vbNullString and "" --------------------------------------------------------------------------------You might have noticed there is an intrinsic constant, vbNullString, that seems to be the same as "". In fact, MsgBox vbNullString = "" displays True. So, what really is the difference. Open VB, and start a new standard exe project. Open the code window and paste this code. Code: Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ (Destination As Any, Source As Any, ByVal Length As Long)Private Sub Form_Click() Dim temp As String, strLen As Long Me.Print VarPtr(temp) & " - " & StrPtr(temp) temp = String(100, " ") CopyMemory strLen, ByVal (StrPtr(temp) - 4), 4 Me.Print VarPtr(temp) & " - " & StrPtr(temp) & " - " & strLen temp = "" CopyMemory strLen, ByVal (StrPtr(temp) - 4), 4 Me.Print VarPtr(temp) & " - " & StrPtr(temp) & " - " & strLen temp = vbNullString Me.Print VarPtr(temp) & " - " & StrPtr(temp) End Sub Run the project and notice the four lines displayed on the form. The first number will always be the same. This is the 4 byte storage address for the BSTR pointer. It can't change as long as the variable is in scope. The second number is the address where the actual string is stored and the third number shows the value stored in the length bytes. The address starts out 0 for an uninitialized string because no storage has been used. As soon as 100 space characters are stored, a chunk of 206 bytes of memory is allocate starting four bytes before the address shown. Strings are Unicode which uses two bytes per character, and there is a two byte Chr$(0) at the end. The four bytes before the address shown contain a Long value that holds the length of the string (not including the 2 byte Chr$(0) at the end). As you can see, once a string is set to "", it has a length of 0, but it is still taking up 6 bytes of memory. Once the string is set to vbNullString, it is restored back to no memory allocated.
MsgBox vbNullString = Space(0)
Hwd = FindWindow("Progman", vbNullString)
vbNullString是否相当于java中"",因为没见对vbNullString的定义
vbCrLf Chr(13) + Chr(10) 回车符与换行符结合
vbCr Chr(13) 回车符
vbLf Chr(10) 换行符
vbNewLine Chr(13) + Chr(10) 平台指定的新行字符;适用于当前平台
vbNullChar Chr(0) 值为 0 的字符
vbNullString 值为 0 的字符串 用来调用外部过程;与长度为零的字符串 ("") 不同
vbObjectError -2147221504 用户定义的错误号应当大于该值,例如:
Err.Raise Number = vbObjectError + 1000
vbTab Chr(9) Tab 字。
vbBack Chr(8) 退格字符
vbFormFeed Chr(12) 在 Microsoft Windows 中没有作用
vbVerticalTab Chr(11) 在 Microsoft Windows 中没有作用
that seems to be the same as "". In fact,
MsgBox vbNullString = ""
displays True. So, what really is the difference.
Open VB, and start a new standard exe project. Open the code
window and paste this code. Code:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)Private Sub Form_Click()
Dim temp As String, strLen As Long
Me.Print VarPtr(temp) & " - " & StrPtr(temp)
temp = String(100, " ")
CopyMemory strLen, ByVal (StrPtr(temp) - 4), 4
Me.Print VarPtr(temp) & " - " & StrPtr(temp) & " - " & strLen
temp = ""
CopyMemory strLen, ByVal (StrPtr(temp) - 4), 4
Me.Print VarPtr(temp) & " - " & StrPtr(temp) & " - " & strLen
temp = vbNullString
Me.Print VarPtr(temp) & " - " & StrPtr(temp)
End Sub
Run the project and notice the four lines displayed on the form.
The first number will always be the same. This is the 4 byte
storage address for the BSTR pointer. It can't change as long as
the variable is in scope.
The second number is the address where the actual string is
stored and the third number shows the value stored in the length
bytes.
The address starts out 0 for an uninitialized string because no
storage has been used. As soon as 100 space characters are
stored, a chunk of 206 bytes of memory is allocate starting four
bytes before the address shown. Strings are Unicode which uses
two bytes per character, and there is a two byte Chr$(0) at the
end. The four bytes before the address shown contain a Long
value that holds the length of the string (not including the 2 byte
Chr$(0) at the end). As you can see, once a string is set to "", it
has a length of 0, but it is still taking up 6 bytes of memory. Once
the string is set to vbNullString, it is restored back to no memory
allocated.