Example: The Error Subtype

Dim datBirth
Dim lngAge
datBirth = InputBox("Please enter the date on which you were born.")
If IsDate(datBirth) Then
lngAge = GetAge(datBirth)
If Not IsError(lngAge) Then
MsgBox "You are " & lngAge & " years old."
Else
If lngAge = 1000 Then
'This error means that the date was greater
'then the current system date.
MsgBox "That date was greater than the current system date."
Else
'An unknown error occured.
MsgBox "The error " & lngAge & " occured in the GetAge()"_
& "function"
End If
End If
Else
MsgBox "You did not enter a valid date."
End If

Keep in mind that GetAge ( ) is a totally fictional function, and you cannot actually run this code. The point here is only to illustrate how someone might use the Error subtype, and how your code might have to respond to it. You could not easily implement the use of the Error subtype yourself in VBScript because the VBScript does not support the CVErr ( ) conversion function, as Visual Basic does. Therefore, without the aid of Visual Basic, you could never coerce the subtype of a variable to be Error.

First

The Error Subtype

The error subtype is seldom used. However, there's a remote chance that you might end up coming across a component or function that uses the Error subtype to indicate that an error occurred in the function. I am not necessarily endorsing this methodology, but what you might encounter is a function that returns a Variant value that will either contain the result of the function, or an error number. Let's say its a fictional function called GetAge ( ) that returns a person's age in years. This function would take a date as a parameter, and return to you the person's age, based on the computer's current system date. If an error occurred in the function, then the return value would instead contain an error number indicating what went wrong. Ponder on the fictional example given in the next post.

Next

Why Destroy an Object?

Why would you want to destroy an object using Set = Nothing syntax? It's a good idea to do this when you are done using an object, because destroying an object frees up the memory it was taking up. Objects take up a great deal more memory than normal variables. Also, for reasons that are too complex to go into here. In short, keeping object variable around longer than necessary can cause fatal memory errors. So, it's a good idea to develop a habit of setting all object variables equal to Nothing immediately after you are done with them.

First

Special Value of 'Nothing'

Nothing is a special value that applies only to variables with the Object subtype. An object variable is equal to the value Nothing when the subtype is Object, but the object in the variable has either been destroyed or has not yet been instantiated. When testing for whether an object variable is equal to the value Nothing, you do not use the = operator, as you normally would to test for a specific value. Instead, you have to use the special operator Is. However, when you wan to destroy an object, you have to use the Set Keyword in combination with the = operator. Let's look at an example:

Dim objFSO
Dim boolExists
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
If IsObject(objFSO) Then
boolExists = objFSO.FileExists("C:/autoexec.bat")
MsgBox boolExists
Set objFSO = Nothing
If objFSO Is Nothing Then
MsgBox "The object has been destroyed"
End If
End If

First
Next

IsObject ( ) Function

Ponder on the following example:

Dim objFSO
Dim boolExists
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
If IsObject(objFSO.FileExists("C:/autoexec.bat")
MsgBox boolExists
End If

This example illustrates the use of the IsObject ( ) funtion, which is similar to the IsNumeric ( ) and IsDate ( ) Functions that I explained in other posts. If the variable holds a reference to an object, then the function will return True. Otherwise, it will return False.

First
Next

FileSystemObject

Here I am going to tell you that how variables with the object subtype behave. Let's look at some code that actually uses a real object, in this case the FileSystemObject, which is part of a collection of subjects that allow your VBScript code to interact with the Windows file system.

Dim objFSO
Dim boolExists
Set objFSO = WScript.CreateOnject("Scripting.FileSystemObject")
boolExists = objFSO.FileExists("C:/autoexec.bat")
MsgBox boolExists

In this code, we create a FileSystemObject object and store it in the variable called objFSO. We then use the FileExists method of the object to test for the existence of the autoexec.bat file. Then we display the result of this test in a dialogue box. Note the use of the Set Keyword. When changing the value of an object variable, you must use Set.

First
Next

Class, Properties, and Methods

You could create a class called "Dog". This Dog class could have properties called "Color", "Breed", and "Name", and it could have methods called "Bark" and "Sit". The class defination would have code to implement these properties and methods. Objects created at runtime from the Dog class would be able to set and read the properties and call the methods. A class typically exists as part of a component. For example, you might have a component called "Animals" that contains a bunch of different classes like "Dog", "Elephant", and "Rhino". The code to create and use a Dog object would look something like this:

Dim objMyDog
Set objMyDog = WScript.CreateObject("Animals.Dogs")
objDog.Name = "Buddy"
objDog.Breed = "Poodle"
objDog.Color = "Brown"
objDog.Bark
objDog.Sit

First
Next

The Object Subtype

As the name suggests, a variable will have the Object subtype when it contains a reference to an object. An object is a special construct that contains properties and methods. A property is analogous to a variable, and an method is analogous to a function or procedure. An object is essentially a convenient way of encompassing both data (in the form of properties) and functionality (in the form of methods). Objects are always created at runtime from a class which is a template from which objects are created (or instantiated).

Next

Empty Strings and Nulls

Here is an example of the type of thing you want to do when you're concerned that a database column might return a Null value:

strCustomerName = rsCustomers.Fields("Name").Value
If IsNull(strCustomerName) Then
strCustomerName = ""
End If

Here we are assigning the value of the "Name" column in a database table to the variable strCustomerName. If the Name column in the database allows Null values, then we need to be concerned that we might end up with a Null value in ur variable. So we use IsNull ( ) to test the value.If IsNull ( ) returns True, then we assign an empty string to the variable instead. Empty strings are much more friendly then Nulls. Here is a handy shortcut that achieves the same exact thing as the above code.

strCustomerName = "" & rsCustomers.Fields("Name").Value

Here we are appending an empty string to the value coming from the database. This takes advantage of VBSctipt's implicit type coercion behavior. Concatenating an empty string with a Null value transforms that value into an empty string, and concatenating an empty string to a valid string has not effect at all, so it is a win-win situation; if the value is Null, it gets fixed, and if it's not Null, it's left alone.

First

Invalid Use of Null

Dim varTest
varTest = Null
varTest = CLng(varTest)

Running this code produces an error on line 3: "Invalid Use of Null". This is a common error with many VBScript functions that don't like Null values to be passed into them. Take a look at the odd behavior that results from this code:

Dim varTest
Dim lngTest
varTest = Null
lngTest = 2 + varTest
MgsBox(lngTest)

Did you see what happened? When we added the number 2 to the value Null, the result was Null. Once again when you mix invalid data (Null) with valid data (the number 2, in this case), you always end up with invalid data.

First
Next

Null and Mathematical Operations

Your code has to be concerned with receiving Null values from a database. The reson I say that you need to be concerned is that, since Null is an indicator of invalid data, Null can cause trouble for you if you pass it to certain functions or try and use it to perform mathematical operations. Try the Code given in next post.

First
Next

Null Example

'This code does not work like you might expect
Dim varTest
varTest = Null
If varTest = Null Then
MsgBox "The variable has a Null value."
End If

You did not see any dialogue box pop up did you? That is because the expression If varTest = Null always returns False. If you want to know if a a variable contains a Null value, you must use the IsNull ( ) function:

If IsNull(varTest) = True Then
MsgBox "The variable has a Null value."
End If

First
Next

Testing Null

The syntx for assigning and testing for Null values is similar to the way the Empty value/subtype works. Here is some code that assigns a Null value to a variable:

varTest = Null

However, you cannot directly test for the value of Null in the same way that you can with Empty - you must use only the IsNull ( ) function to test for a Null value. This is because Null represents invalid data, and when you try to make a direct comparison using invalid data, the result is always invalid data. Try running the code given on next post.

First
Next

Null From Another Angle

Another way to think about it is that Empty generally happens by default - it is implicit, because a variable is Empty until you place something in it. Null, on the other hand, is explicit - a variable can only be Null, if some code made it that way.

First
Next

Null

The value/subtype of Null, in a confusing way, is similar to the value/subtype of Empty. The distinction may seem esoteric, but Empty indicates that a variable is uninitialized, whereas Null indicates the absence of valid data. Empty means that no value has been placed into a variable, whereas a Variant variable can only have the value/subtype of null after the value of Null has been placed into it. In other words, a variable can only be Null if the Null value has explicitly been placed into it. Null is a special value that is most often encountered in database tables. A column in a database is Null when there is no data in it, and if your code is going to read data from a database, you have to ready for Null values.

First
Next

Test Empty

You can also test for whether a variable is empty in either of two ways:

If varTest = Empty Then
MsgBox "The variable is empty."
End If

OR:

If IsEmpty(varTest) Then
MsgBox "The variable is empty."
End If

The IsEmpty ( ) function returns a Variant value of the Boolean subtype with the value of True if the variable is empty, False if not.

First

Empty Example 2

Dim varTest
MsgBox CLng(varTest)
MsgBox CStr(varTest)

When you run this script, it will produce two dialogue boxes. The first box displays a 0 because Empty is 0 when represents as a number. The second box displays nothing because Empty is an "empty" or "zero length" string when represented as a String.

Once you place a value in a variant variable, it is no longer empty. It will take on another subtype, depending on what type of value you place in it. This is also true when you use a conversion function to coerce the subtype. However, if you need to, you can force the variable to become empty again by using the Empty keyword directly:

varTest = Empty

First
Next

Empty Example 1

Dim varTest
MsgBox TypeName(varType)

Run the script using Windows Script Host and you'll see the value as "Empty". The subtype is Empty because we have not yet placed any value in it. Empty is both the initial subtype and the initial value of the variable. However, Empty is not a value that you can really do nothing with. You can display it on the screen or print it on paper. It only exists to represent the condition of the variable not having had any value placed in it. Try the code on next page.

First
Next

Empty

Empty is a special value that can only be held in a Variant variable. In Visual Basic, variables declared as any of the specific data types cannot hold the value of Empty-only variables declared as Variant. In VBScript, of course, all variables are Variant variables. A Variant variable is "empty", and has the Empty subtype, after it has been declared, but before any value has been placed within it. In other words, Empty is the equivalent of "not initialized". Once any type of value has been placed into the variable, it will take on one of the other subtype, depending on what the value is. Let us take a look at some examples:

First
Next

Empty and Null

Empty and Null are special in that they do not have a corresponding specific Visual Basic data type. In fact, it is a bit of misnomer to call these subtypes, because they are actually special values that a Variant variable can hold. When the subtype of a variable is Empty or Null, its value is also either Empty or Null. This is different then the other subtypes, which only describe the type of value that the variable holds, not the value itself. For example, when the subtype of a variable is Long, the value of the variable can be 0, or 15, or 2,876,456, or one of about 4.3 billion numbers (-2,147,483,648 to 2,147,483,647). However, when the subtype of a variable is Empty, it's value is also always a special value called Empty. In the same fashion, when the subtype of a variable is Null, the value is always a special value called Null.

Empty
Null