I recently needed to check for the existance and type of a VBScript object returned from a function. Perhaps you need to too.
I thought there must be a standard property of all objects, like, maybe, object.type or object.size or object.id or something. But I couldn't dig up such a thing. I needed to check what kind of object was being returned - a Microsoft.xmlhttp XML object, or a "Nothing" object....
My "GetXMLObjectViaHTTP" function it was grabbing some xml and looking for errors like so:
If Err.number = 0 Then
Set GetXMLObjectViaHTTP = objXMLDocument
Set GetXMLObjectViaHTTP = nothing
If an error occurred in the function I had to set the return type to "nothing" which is an object (apparently). I originally set the return value as an error number, then just "", but both wont work because of how I'm using the object returned:
Set objXML = GetXMLObjectViaHTTP( strXMLUrl )
You end up with Set objXML = "-1", giving the error "
Object required: '[number: -1]'"
I was going to make a customer "error" class to handle it (which I ended up doing anyway...), but first I had a look around for other's solutions. I couldn't find anything, though eventually came across a VBScript function "TypeName()". Running "
Response.write( TypeName( objXML ) )" gives:
"DOMDocument" if it was okay, and "Nothing" if it something broke. Which is like, totally testable.
I'm too lazy to look up when TypeName was introduced to VBScript (maybe since forever), but the server I'm testing on is version 5.7426.
UPDATE: I found a couple-a more cool things: VarType() returns a type code, rather than a name. Google for the type codes, they're everywhere unlike this function name.
Also, I never realised that you could do
If objMyObject Is Null. I thought "Is Null" was only a VB construct. Anyhoo, thats useful in object detecter-ing too.
Thanks alot. I had this exact issue and this has given me a was to solve it.
TypeName has been around since version 2.0. Here’s a helpful lookup chart of when different VB Script features were implemented (need to scroll down a little):
This post was like, totally helpful.