Article ID: 129388
Article Last Modified on 12/9/2003
Public sNull As String Public Const sEmpty = ""If you look at these two strings in the Watch window, they look exactly the same; both displayed as "". You can use them in almost the same contexts, but internally they are very different.
const char *sNull = NULL;Internally, sEmpty is a pointer to an empty string. It is a valid pointer to some memory location. In C, you would code it as:
const char sEmpty[] = "";All Visual Basic version 4.0 variables are set to zero (0) until initialized. In previous versions of Visual Basic, uninitialized variable-length strings were automatically set to an empty string (""). Therefore, for compatibility with previous versions, you might think that you must initialize string variables to empty strings. However, Visual Basic version 4.0 strings are in the BSTR format where a null pointer is defined to behave exactly as an empty string does. Therefore, in Visual Basic version 4.0, you can leave the initial zero value of an uninitialized strings alone because it will behave as if it were an empty string.
Public Const sNull As String = 0&However, Visual Basic does automatic numeric conversion on this and converts it to zero (0), which is neither an empty string nor a null string pointer.
Const sEmpty = ""
Dim sNull As String
Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As Any, ByVal _
lpWindowName As Any) As Long
Shell "Calc.exe", 1
DoEvents
x& = FindWindow(sNull, "Calculator")
'x& = FindWindow(sEmpty, "Calculator")
Debug.Print x&
x& = FindWindow(sNull, "Calculator")
And change the following line from a comment into executed code:
'x& = FindWindow(sEmpty, "Calculator")
Then run the program again. You will see a value of zero (0) printed in
the Debug Window, indicating that FindWindow failed. This happens
because sEmpty is not a null string pointer.Keywords: kbinfo kbprogramming KB129388