AndyS01 Posted April 30, 2016 Share Posted April 30, 2016 (edited) I have a script that calls _ExtMsgBox() from time to time to show progress through the test script by displaying the current line number and I'm getting a subscripting error. My script makes this call: _ExtMsgBox($MB_ICONINFORMATION, "OK", "Test", @ScriptLineNumber) The error was: "C:\Andy\AutoIT-src\myLib\_ExtMsgBox.au3" (787) : ==> Subscript used on non-accessible variable.: _ExtMsgBoxSet(0, 0, -1, -1, 10, "Courier New", $aRet[2] + 70) _ExtMsgBoxSet(0, 0, -1, -1, 10, "Courier New", $aRet^ ERROR I traced it to a problem in _StringSize() where it returns an error if the first parameter is not a string. The code in _ExtMsgBox() does not check for an error and assumes that _StringSize() returns an array. However, it returns a 0, causing the subssript error. The problem is that because AutoIT has non-typed variables, a text string of all digits and a numeric value both fail the IsString() test. For example, both of these calls fail: Quote _StringSize(123456, ...) _StringSize("123456",...) So there really are 2 issues here: 1) The _ExtMsgBox() code does not check for an error return from _StringSize() 2) _StringSize() fails when a string containing all digits is passed as the first parameter. I commented out the "if not isString" test and it all worked OK. I don't know why this check is there as a call with a number as the first parameter just displays the number as a string of digits. Edited April 30, 2016 by AndyS01 Link to comment Share on other sites More sharing options...
AndyS01 Posted April 30, 2016 Author Share Posted April 30, 2016 (edited) Sorry, I forgot to say that I was calling a wrapper function that calls _ExtMsgBox(). This function parses the string and passes a "$iWidth" parameter adjusted for a fixed width font. This is in response to this issue. Here is the function: expandcollapse popup; #FUNCTION# ==================================================================================================================== ; Name ..........: _ExtMsgBoxFWFromStr ; Description ...: Calls _ExtMsgBox() to display a @CRLF separated string in a fixed font gui ; Syntax ........: _ExtMsgBoxFromFWStr($vIcon, $vButton, $sTitle, Byref $ar) ; Parameters ....: $vIcon - Icon to use ; $vButton - Button text separated with "|" character. " " = no buttons. ; $sTitle - The title of the message box. ; $ar - [in/out] An array text strings. ; $iTimeOut - See _ExtMsgBox() for details about this parameter ; $hWin - See _ExtMsgBox() for details about this parameter ; $iVPo - See _ExtMsgBox() for details about this parameter ; $bMai - See _ExtMsgBox() for details about this parameter ; Return values .: None ; Author ........: Andy Scharmer ; Modified ......: ; Remarks .......: See _ExtMsgBox.au3 for details about the calling parameters. ; Related .......: ; Link ..........: ; Example .......: No ; $vIcon, $vButton, $sTitle, $sText, $iTimeOut = 0, $hWin = "", $iVPos = 0, $bMain = True ; =============================================================================================================================== Func _ExtMsgBoxFromFWStr($vIcon, $vButton, $sTitle, $sString, $iTimeOut = 0, $hWin = "", $iVPos = 0, $bMain = True, $lnum = @ScriptLineNumber) #forceref $lnum Local $aRet ; Size the string $aRet = _StringSize($sString, 10, Default, Default, "Courier New") If ($aRet == 0) Then Local $err = @error Local $ext = @extended _DebugOut("+++:" & $lnum & ": [" & "_ExtMsgBoxFromFWStr" & "] @error = " & $err & ", @extended = " & $ext) ;;;debugging;;;Exit;;;debugging;;; EndIf ; Now set the ExtMsgBox to a width which will accommodate the widest line _ExtMsgBoxSet(0, 0, -1, -1, 10, "Courier New", $aRet[2] + 70) ; 70 = 50 for the icon + 2 borders of 10 ; And it fits!!! _ExtMsgBox($vIcon, $vButton, $sTitle, $sString, $iTimeOut, $hWin, $iVPos, $bMain) EndFunc ;==>_ExtMsgBoxFromFWStr I made the $aRet return check after getting subscripting errors. So the only issue is with _StringSize returning an error if the first parameter is numeric Edited April 30, 2016 by AndyS01 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 30, 2016 Moderators Share Posted April 30, 2016 (edited) AndyS01, Quote _StringSize() fails when a string containing all digits is passed as the first parameter. No, _StringSize fails when passed a numeric value as the first parameter - and it fails gracefully by setting @error to 1 (and even @extended to 1 to indicate which parameter is erroneous): #include "StringSize.au3" _StringSize(1234567) ConsoleWrite(@error & " - " & @extended & @CRLF) You have created a wrapper function which relies on _StringSize returning an array - that is your mistake not that of the UDF (and myself as the author). Quote The _ExtMsgBox() code does not check for an error return from _StringSize() It most certainly does! You will see that _ExtMsgBox itself returns an error when you pass it a digit instead of a string: #include "ExtMsgBox.au3" $sString = 1234567890 _ExtMsgBox(0, "Test", "Title", $sString) ConsoleWrite(@error & @CRLF) And if you look at the error messages for the UDF you see: Failure: Returns -1 and sets @error as follows: ;[...] ; 6 - StringSize error So if you want to write a wrapper function using my UDFs, it is up to you to check the error messages that might be returned and deal with them as appropriate. Or force the required value into string format so that it will work - although you should still check for errors as a matter of course. Ball very firmly returned. M23 Edited April 30, 2016 by Melba23 Added a few more comments Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
AndyS01 Posted May 1, 2016 Author Share Posted May 1, 2016 My mistake, sorry. My wrapper UDF now checks for the error return. However, I'm still wondering why _StringSize() calls a string that's all digits an error. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 1, 2016 Moderators Share Posted May 1, 2016 AndyS01, Quote wondering why _StringSize() calls a string that's all digits an error Because what you keep calling a "string of digits" is actually a numeric value - you need surrounding quotes to make it into a literal string as far as AutoIt is concerned. As you can see from this snippet, when you pass a numeric value (no quotes) you get an error returned - when you pass a string (with quotes) you get no error and the ExtMsgBox displays: #include "ExtMsgBox.au3" _StringSize(1234567) ConsoleWrite("Numeric value: " & @error & " - " & @extended & @CRLF) _StringSize("1234567") ConsoleWrite("Literal string: " & @error & " - " & @extended & @CRLF) $sString = 1234567890 _ExtMsgBox(0, "Test", "Title", $sString) ConsoleWrite("Numeric value: " & @error & @CRLF) $sString = "1234567890" _ExtMsgBox(0, "Test", "Title", $sString) ConsoleWrite("Literal string: " & @error & @CRLF) And StringSize returns an error when passed a numeric value because I coded it that way. The clue is in the name (StringSize) - it is designed to size strings, not numeric values. The function header clearly states: ; $sText - String to display so it is up to the user to pass a string when using the function. In my opinion it is not up to UDF authors to cope with every possible parameter datatype - it is up to the user to force the correct datatype if necessary. In this case I do at least offer a specific @error/@extended return to indicate that there is a problem with that parameter if a string is not passed. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
AndyS01 Posted May 1, 2016 Author Share Posted May 1, 2016 _ExtMsgBox($MB_ICONINFORMATION, "OK", "Test", "" & @ScriptLineNumber) OK, I see. I changed my calls from: _ExtMsgBox($MB_ICONINFORMATION, "OK", "Test", @ScriptLineNumber) to: _ExtMsgBox($MB_ICONINFORMATION, "OK", "Test", "" & @ScriptLineNumber) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 1, 2016 Moderators Share Posted May 1, 2016 AndyS01, I would have done this: _ExtMsgBox($MB_ICONINFORMATION, "OK", "Test", String(@ScriptLineNumber)) and ensured the correct datatype rather than trying to fool the interpreter into thinking it is a string. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
AndyS01 Posted May 1, 2016 Author Share Posted May 1, 2016 Good idea. Why is a numeric parameter invalid to the UDF? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 1, 2016 Moderators Share Posted May 1, 2016 AndyS01, 10 hours ago, Melba23 said: StringSize returns an error when passed a numeric value because I coded it that way M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
AndyS01 Posted May 3, 2016 Author Share Posted May 3, 2016 You mean that you randomly decided that a numeric parameter was an error, or would a numeric parameter cause some other error? I commented out the 'if' statement and it displayed OK. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 3, 2016 Moderators Share Posted May 3, 2016 AndyS01, I did not "randomly" decide that the UDF would error on a numeric parameter - it was a conscious design decision on my part given that the rationale behind the UDF was to size a string. The code must check that the passed parameter is a datatype that would be accepted by the GetTextExtentPoint32W DLL call (i.e. not an array, an object, etc), so it seemed simplest to have just a single check and only accept strings, rather than carry out multiple checks to (hopefully) weed out all the other multiple possibilities that would fail. The fact that AutoIt internally converts a number to a string because of the code that is inside the UDF is not documented and so cannot be relied upon - any future release may not act in the same manner and so cause the UDF to fail without the current @error return that I have kindly provided. And I am becoming a little irritated by the tone you have adopted throughout this thread. This UDF is one of my personal include files which I make freely available to other forum users and as such I am perfectly at liberty to make it behave in any way I wish. Your continual assertions that I have somehow coded it "wrong" have absolutely no justification and have frankly become tiresome. If you wish to modify the StringSize code to make it conform to your requirements then feel free to do so, but do not expect any support from me when it starts misbehaving. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now