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