Jump to content

Recommended Posts

Posted (edited)
#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. autoit_scripter_blue_userbar.png

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 :) 

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...