VBscript Is Frustrating

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...

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

About Erich

Erich is a web developer and a native New Englander who is passionate about life, the universe, and everything.

He is a Drupal consultant, previously employed as a senior developer at Harvard University, working on the IQSS OpenScholar project.  Prior to joining the team at Harvard, he was the engineering manager at CommonPlaces e-Solutions, in Hampstead, NH, contributing as the lead engineer on the Greenopolis.com and Twolia.com.

Erich is active in the Drupal community, having contributed modules and patches to the community. He presented at DrupalCon in Szeged Hungary, and co-presented at DrupalCon 2009 in Washington, DC.

Erich lives in New Hampshire with his wife, two sons, and three weimaraners.  When not writing code, Erich enjoys landscaping and woodworking.

Faceted search

Categories

Content type

Project types

Artwork Type

Artwork Tags

Recent comments

Activity Stream

August 29, 2011

August 25, 2011

August 24, 2011

August 23, 2011

August 15, 2011

August 11, 2011

August 10, 2011

August 9, 2011

August 4, 2011

August 3, 2011