How to Check if an Object is a Collection

Viewed 4583

I keep getting this error in one of my scripts:

Object is Not a Collection

So I would like to create a function which checks whether or not the object is a collection before performing any operations on it.

So far I have created this:

Function IsCollection(param)
    If IsEmpty(param) Then
        IsCollection = False
    ElseIf(TypeName(param) = "String") Then
        IsCollection = False
    Else
        IsCollection = True
    End If
End Function 

But I get the feeling I am missing some checks - surely the only available types aren't just String, Empty or Collection?

I thought it could be better to just try and enumerate param and if this returns an error, then I know to output false - does this seem like a better alternative?

Function IsCollection(param)
    For Each p In param
        ' Anything need to go here?
    Next

    If Err > 0 Then
        IsCollection = False
    Else
        IsCollection = True
    End If
End Function 

Even in the second example, would it be wise to specify whether the error given is "Object is not a collection"?

1 Answers
Related