I can't stand the way VBScript handles non-values. Consider the following:
<pre><xmp>
strValue = SomeFunction()
If strValue = "" Then
' Do Something
Else
' Do Something Else
End If
</xmp></pre>
This fails unless your SomeFunction() function specifically returns an empty string, such as:
<pre><xmp>
strValue = SomeFunction()
If strValue = "" Then
' Do Something
Else
' Do Something Else
End If
Function SomeFunction()
SomeFunction = ""
End Function
</xmp></pre>
This is because even an empty string is considered a value in VBScript, on which you can check the length and check for IsEmpty(). However, this doesn't work if your function returns a variable that has no value, like this:
<pre><xmp>
strValue = SomeFunction()
If strValue = "" Then
' Do Something
Else
' Do Something Else
End If
Function SomeFunction()
strValue = SomeOtherFunction()
SomeFunction = strValue
End Function
</xmp></pre>
In this case, if the SomeOtherFunction() did not return a value, then the value of strValue is NULL. In VBScript, a NULL is defined as "no valid data." So, the only way to validate this variable is to check for NULL:
<pre><xmp>
strValue = SomeFunction()
If IsNull(strValue) Then
' Do Something
Else
' Do Something Else
End If
Function SomeFunction()
strValue = SomeOtherFunction()
SomeFunction = strValue
End Function
</xmp></pre>
Because strValue does not hold valid data, IsNull() evaluates to TRUE. Now, in the case of objects, where you expect a function to return an object, you have to use a completely different method:
<pre><xmp>
Set objValue = SomeFunction()
If objValue Is Nothing Then
' Do Something
Else
' Do Something Else
End If
Function SomeFunction()
Set objValue = SomeOtherFunction()
Set SomeFunction = objValue
End Function
</xmp></pre>
Strange, and not entirely intuitive, but there it is...
Recent comments
18 hours 18 min ago
1 week 6 days ago
1 week 6 days ago
1 week 6 days ago
1 week 6 days ago
1 week 6 days ago
2 weeks 1 day ago
4 weeks 1 day ago
10 weeks 3 days ago
16 weeks 6 days ago