Thursday, June 25, 2009

VBScript Function IsArrayValid()

I see a lot of people still stuck in VBS-land that are frustrated by the lack of array handling functions. One of the biggest questions is how to deal with uninitialized dynamic arrays. You can't depend on ubound() = 0 because by default VBS arrays are zero-based and if you try to do a ubound() on an array that hasn't been Dim/Redim-ed it will throw an error. IsArray() returns true and isNull() or isEmpty() return false regardless of whether the array has been initialized or not. For anyone who's interested I put together this very simple function you can use to deal with it. Like it, use it, or don't - it's up to you.

Function isArrayValid(varArray)
Dim varResult
Dim intTemp

varResult = False
intTemp = -1

On Error Resume Next
intTemp = ubound(varArray)
On Error Goto 0

If intTemp > -1 then
varResult = True
End If

isArrayValid = varResult
End Function

0 Comments:

Post a Comment

<< Home