LotusScript Gurus! How to Check for unitialized array?

Hey all,

I am passing a dynamic array to a function that is optional.

I need to check to see if in fact there are values in the passed array.

I have tried using IsEmpty() and IsNull (returns false on an empty array), Is Nothing (gives Type Mismatch on compile) etc but can’t seem to lick this one.

I can of course just perform an operation and trap error “200” - attempt to access an uninitialized array, but would like to know if I can test for that condition before actually triggering the error.

Subject: Solution - Kinda…

First of all, thanks for the responses!

As Ceasar mentioned, performing an operation such as a ubound can trigger error 200 which lets you know that the array wasn’t initialized. In the end I just created a function to handle this so the error would stay clean. Here’s the code in case anyone else ever finds it useful.

Unfortunately, I need to know the type at the time of passing the array. Trying to pass it to a variant will trigger another error altogether. So, if you want to test different types of Arrays, (string, integer, etc), you need to create a separate function for each.

Function IsUnInitializedStringArray( arrayToTest () As String ) As Boolean

IsUnInitializedStringArray = False



On Error Goto ErrorThrower



On Error 200 Goto HandleUninitializedStringError



' trying to get the ubound of an uninitialized array will cause an error 200 to trigger



Dim x As Integer



x = Ubound ( arrayToTest () )

Done:

Exit Function

HandleUninitializedError:

IsUnInitializedStringArray= True

Resume Done

ErrorThrower:

Error Err, Error & Chr(13) + "Module: " & _

Cstr( Getthreadinfo( LSI_THREAD_PROC ) ) & ", Line: " & Cstr( Erl )	

End Function

Subject: RE: Solution - Kinda…

Wouldn’t it be sufficient to define the parameter arrayToTest as Variant instead of a string array? Tpyename would still return the type of what you passed into your function, if you need that info.

Subject: RE: Solution - Kinda…

Why not use ForAll loop to test whether an array has elements or not.

Dim x() as string

Dim isArr as integer

ForAll v in x

isArr = True

End Forall

Msgbox cstr(isArr)

HTH

Adi

Subject: That works too, but I would do this:

Dim x() as stringDim isArr as integer

ForAll v in x

isArr = True

Exit Forall 'Imagine if you have 10,000 elements in the array :slight_smile:

End Forall

Msgbox cstr(isArr)

Subject: Function IsArrayEmpty

In a function this is my version:

Function IsArrayEmpty(parArr As Variant)As Boolean

IsArrayEmpty=true



ForAll v In parArr

	IsArrayEmpty=false

	Exit function

End ForAll

End Function

Subject: RE: Function IsArrayEmpty

Thanks Johann

Subject: LotusScript Gurus! How to Check for unitialized array?

Try this…

Dim varTempArray As Variant

varTempArray=Fulltrim(arrYourArray)

If varTempArray(0) = “” Then

 Msgbox "Uninitilized Array"

Else

 Msgbox "Initilized Array"

End If

Subject: LotusScript Gurus! How to Check for unitialized array?

I normally check the array’s UBounds but use an error trap because the bounds check will also fail on an uninitialized array.

IsEmpty is for variants. And IsNull will be true if you specifically set a variant to Null. Nothing is used only for objects (or variants that may be assigned to an object).

Subject: LotusScript Gurus! How to Check for unitialized array?

did you try ‘IsArray()’?

Subject: RE: LotusScript Gurus! How to Check for unitialized array?

IsArray will fail to give the correct answer. Try:

Dim x() as String

MsgBox IsArray(x)

Subject: RE: LotusScript Gurus! How to Check for unitialized array?

Use TypeName(x). It works for all data types and never crashes.

Subject: RE: LotusScript Gurus! How to Check for unitialized array?

This returns the type of the array but doesn’t establish if it’s been initialized (ReDim’ed) or not.

Subject: RE: LotusScript Gurus! How to Check for unitialized array?

IsArray returns true even if the Array is empty so that doesn’t do it.