Reprinted: (Original post) Unique NULL, EMPTY, NOTING, AND VBNULLSTRING
Some of these strange values have some subtle differences:
NULL: NULL is a variable type like Integer or String, which represents a variable without legitimate data. This is not to Zero, Nothing, Empty or VBnullString. Many values are combined with NULL and will produce null results. such as:
Expression results
Null - Null Null
NULL 7 7
Null = null null
You can use the ISNULL statement to determine if the expression is NULL:
IF isnull (my_variable) then ...
Empty: This is also a variable type like Integer or String, which indicates a variable that has not been initialized yet. It is different from NULL, and NULL indicates that there is no legal data.
A value of a variable without initialization is EMPTY. You can use the ISEMPTY statement to determine whether the variable is initialized:
ISEMPTY (my_variant) Then ...
Nothing: This is an object reference to the empty object. The object is set to Nothing, which is released. If there is no other reference pointing to the object, Visual Basic will destroy this object.
DIM OBJ AS FORM1
:
Set obj = Nothing Note: free the object reference.
Use the is nothing statement to determine if a reference is Nothing:
IF obj is nothing then ...
vbnullstring: This is a constant, represents an EMPTY string. It differs from the blank string "", indicating what nor is nothing string. For many occasions, it is treated as an EMPTY string "" process, and the purpose of truly use it is to pass the NULL parameter to the library function.
NULL is a very strange value, it is not Zero, not nothing, not vbnullstring. It is no defined thing.
Determine if the string is blank
There are several ways to determine if a string is blank:
DIM TXT AS STRING
Dim blank as string
Blank = ""
:
IF len (txt) = 0 THEN ...
IF txt = vbnullstring the ...
IF txt = "" "..."
IF txt = blank the ...
After testing, the method of len (txt) = 0 is more than 20% faster than other methods.
Search from string tailings
In Visual Basic Release 6, you can use the InstrRev function to achieve the function of searching the matching substrings from the tail of the string. In the early versions, there is no such function, as a replace, please use the following code:
Function lastinstr (txt as string, pattern as string)
DIM POS1 AS INTEGER
DIM POS2 AS INTEGER
POS2 = 0
DO
POS1 = POS2
POS2 = INSTR (POS1 1, TXT, PATTERN)
Loop While Pos2> 0
Lastinstr = POS1
END FUNCTION