INFO: How Null String Pointers Behave in Visual Basic 4.0Last reviewed: September 29, 1997Article ID: Q129388 |
The information in this article applies to:
SUMMARYVisual Basic version 4.0 recognizes two very different kinds of strings that look the same but act differently. One is a Null pointer string, the other is an empty string. You might code them in a public module as follows:
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.
MORE INFORMATIONInternally, sNull is a null pointer. It does not point to any memory location and has a value of zero. In C, you would code it as:
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. This means that sNull can now be passed to any Windows API function that takes a Null pointer. This is something that was not possible in previous versions of Visual Basic. For example, it can be passed to FindWindow, which gets the handle of a window, given either its class name or its title, or both. However for this to work, sNull must be passed ByVal. In general, for FindWindow (or any other Windows API) to work, the Declare statement must be written to pass the string ByVal As String or ByVal As Any. If the string is passed by reference, a pointer to a BSTR would then be passed, which is nothing but a pointer to a pointer to char. This will not work as Windows APIs expect strings that are pointers to char. NOTE: In Visual Basic version 4.0, sNull might also be expected to be equivalent to:
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.
Step-by-Step Example
Keywords : APrg APrgOther VB4ALL VB4WIN vbwin GnrlVb kbprg Technology : kbvba Version : WINDOWS:4.0 Platform : WINDOWS Issue type : kbinfo |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |