11-08-2022 02:37 AM
Hi,
I can't get a piece of code to work that tries to determine whether a dynamic array has been used or not.
A dynamic array is defined at the head of the code: VarArray(). If any of the batch of files being processed don't meet certain criteria then VarArray is redimensioned and the suspect file name added to it. However, if all the files are valid then VarArray is never redimensioned.
Later in the code I wish to extract the contents of VarArray for a message box so I need to interrogate it to see if it has any contents. I have tried all the following and get either an error message or the line is ignored and the IF code entered:
If IsNull(VarArray) = False Then
If IsNull(VarArray()) = False Then
If IsNull(VarArray(0)) = False Then
If IsEmpty(VarArray) = False Then
If IsEmpty(VarArray()) = False Then
If IsEmpty(VarArray(0)) = False Then
I have also tried If Not etc... in case that made any difference. It didn't.
If I watch the Current Variable Contents window then VarArray always shows {...} when unused. How do I correctly define the code to determine whether VarArray was ever redimensioned?
Regards.
Solved! Go to Solution.
11-08-2022 03:13 AM
By way of an update, and using the example from the IsEmpty help page:
Dim MyVar, MyVar2()
Call MsgBox(IsEmpty(MyVar)) 'Returns True
Call MsgBox(IsEmpty(MyVar2)) 'Returns False
It seems IsEmpty behaves differently with dynamic arrays. Why is this and what can I do to circumvent this behaviour?
Regards.
11-09-2022 12:47 AM
Hi Simon_Aldworth,
You can initialize the dynamic array with 0, then you can askas follows:
dim VarArray()
redim VarArray(0)
if isarray(VarArray) then
if uBound(VarArray) = 0 then msgbox "No change"
redim VarArray(1)
if uBound(VarArray) > 0 then msgbox "Array size is: " & uBound(VarArray)
end if
Greetings
Walter
11-09-2022 02:25 AM
Thanks Walter.
It would be great if the IsEmpty behaviour was consistent for different variable types, or at least a note in the helps files saying it behaves differently with dynamic arrays.
Regards.
11-09-2022 02:30 AM
Hi Simon_Aldworth,
Yes, I agree, but this must be discussed with Microsoft because IsEmpty is a VBS function and no DIAdem function.
Greetings
Walter