#include <WinAPIProc.au3> ; for the example
Func MustReturnArray($aArray, $iColumns = 0, $iErr = @error, $iExt = @extended)
If UBound($aArray) Then Return SetError($iErr, $iExt, $aArray)
If $iColumns Then
Dim $aArray[1][$iColumns] = [[0]]
Else
Dim $aArray[1] = [0]
EndIf
Return SetError($iErr, $iExt, $aArray)
EndFunc ;==>MustReturnArray
Exit MustReturnArray_Example()
Func MustReturnArray_Example()
; worry free return
Local $aWinList = WinList("nah, is not there")
For $n = 1 To $aWinList[0][0]
ConsoleWrite($aWinList[$n][1] & @TAB & $aWinList[$n][0] & @CRLF)
Next
; worry free return
Local $aProcessList = ProcessList("nah, is not there")
For $n = 1 To $aProcessList[0][0]
ConsoleWrite($aProcessList[$n][1] & @TAB & $aProcessList[$n][0] & @CRLF)
Next
; solution ; create a "worry free return"
Local $aArray = MustReturnArray(_WinAPI_EnumProcessWindows(4), 2)
For $n = 1 To $aArray[0][0]
ConsoleWrite($aArray[$n][1] & @TAB & $aArray[$n][0] & @CRLF)
Next
; problem ; not a "worry free return"
Local $aArray = _WinAPI_EnumProcessWindows(4)
For $n = 1 To $aArray[0][0]
ConsoleWrite($aArray[$n][1] & @TAB & $aArray[$n][0] & @CRLF)
Next
EndFunc ;==>MustReturnArray_Example
Before I start with my problem ( and solution ), a big thank you to everyone coding these UDFs. AutoIt would not be what it is without them.
... a coder should always check @error, but is a pain, or not.
In any case, the internal AutoIt functions return an array even if nothing there when an array is what it returns on success.
Am so used to it, that when I use a UDF function I have to RTFM and I don't like reading ( nor writing but here we are 🤷♂️ )
How should I solve this problem given my coding style that does not include a failing function returning a zero stead of the array I was expecting. AutoIt does it, why does the UDFs not do it ?, ... must be a professional coders thing but, am a scripter. I write scripts.
So, if it should return an array, then MustReturnArray() !.
I hope this idea/function helps you avoid the " ==> Subscript used on non-accessible variable. " and/or simplify your code