junkew Posted July 23, 2014 Share Posted July 23, 2014 In december 2013 AutoIt introduced a nice way of putting functions in variables. In which scenarios are those usefull and any documentation in examples of usage in a meaningfull functional way Example I came up with ;~ Basic example $msgbox=msgbox $msgbox(0, "Header", "nice" ) $msgbox=mymsgbox $msgbox(0, "Header", "nice" ) ;~ Debug $debug=myDebugtoMsgBox $debug("Hello we reached this point 1 in the source") $debug=myDebugtoConsole $debug("Hello we reached this point 2 in the source") $debug=myDebugOff $debug("Hello we reached this point 3 in the source") func myMsgBox($v1,$v2,$v3) consolewrite($v3 & @crlf) EndFunc func myDebugOff($v1) return true EndFunc func myDebugtoMsgBox($v1) msgbox(0, "Debug", $v1) EndFunc func myDebugToFile($v1) EndFunc func myDebugToConsole($v1) consolewrite($v1 & @CRLF) endfunc I can imagine usefull overloading for browsers like $browserOpen=BrowserOpenChrome $browserOpen=BrowserOpenIE $browserGetHTML=BrowserGetHTMLChrome $browserGetHTML=BrowserGetHTMLIE Would like to see some usages how others are using this function in variable reference FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
guinness Posted July 23, 2014 Share Posted July 23, 2014 Search on google for "function delegates". You will also see in the new beta that _ArrayMin/MaxIndex() uses one base function and then passes a simple function for the comparison in the wrapper functions. This is correct usage. What you suggested is just for naming only and has no real "benefit" to the script nor demonstrates the correct usage of this wonderful feature. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
junkew Posted July 23, 2014 Author Share Posted July 23, 2014 ok looked around a little and summarized its just a function pointer as in old C days and if done with multilinked delegate lists it can be powerfull. Will download the Beta and see if we can do a lot of wonderfull things with this delegate function in a variable. Regarding your comment about incorrect use I think there is no incorrect use in programming just a best practice that over time will be adopted by multiple developers and only then conclusion can be made if something is incorrect usage. FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
guinness Posted July 24, 2014 Share Posted July 24, 2014 Also look in my signature for _GUIRegisterMsgEx(). UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
LarsJ Posted July 24, 2014 Share Posted July 24, 2014 Here is an example. Traditional code:expandcollapse popup#include <GUIConstantsEx.au3> Opt( "MustDeclareVars", 1 ) Example() Func Example() Local $hGui, $idButton1, $idButton2, $idButton3, $idButton4 $hGui = GUICreate( "$Functions", 300, 200 ) $idButton1 = GUICtrlCreateButton( "Button 1", 20, 40, 120, 40 ) $idButton2 = GUICtrlCreateButton( "Button 2", 160, 40, 120, 40 ) $idButton3 = GUICtrlCreateButton( "Button 3", 20, 120, 120, 40 ) $idButton4 = GUICtrlCreateButton( "Button 4", 160, 120, 120, 40 ) GUISetState() Local $iMsg While 1 $iMsg = GUIGetMsg() Switch $iMsg Case $idButton1 Button1() Case $idButton2 Button2() Case $idButton3 Button3() Case $idButton4 Button4() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd GUIDelete( $hGui ) Exit EndFunc Func Button1() MsgBox( 0, "", "Button 1" ) EndFunc Func Button2() MsgBox( 0, "", "Button 2" ) EndFunc Func Button3() MsgBox( 0, "", "Button 3" ) EndFunc Func Button4() MsgBox( 0, "", "Button 4" ) EndFuncA problem here is that the more controls you add, the more time will be used testing Case statements.This problem is eliminated here:expandcollapse popup#include <GUIConstantsEx.au3> Opt( "MustDeclareVars", 1 ) Example() Func Example() Local $hGui, $idButton1, $idButton2, $idButton3, $idButton4 $hGui = GUICreate( "$Functions", 300, 200 ) $idButton1 = GUICtrlCreateButton( "Button 1", 20, 40, 120, 40 ) $idButton2 = GUICtrlCreateButton( "Button 2", 160, 40, 120, 40 ) $idButton3 = GUICtrlCreateButton( "Button 3", 20, 120, 120, 40 ) $idButton4 = GUICtrlCreateButton( "Button 4", 160, 120, 120, 40 ) Local $aEventHandler[$idButton4+1] $aEventHandler[$idButton1] = Button1 $aEventHandler[$idButton2] = Button2 $aEventHandler[$idButton3] = Button3 $aEventHandler[$idButton4] = Button4 GUISetState() Local $iMsg While 1 $iMsg = GUIGetMsg() If $iMsg > 0 Then $aEventHandler[$iMsg]() ElseIf $iMsg < 0 Then Switch $iMsg Case $GUI_EVENT_CLOSE ExitLoop EndSwitch EndIf WEnd GUIDelete( $hGui ) Exit EndFunc Func Button1() MsgBox( 0, "", "Button 1" ) EndFunc Func Button2() MsgBox( 0, "", "Button 2" ) EndFunc Func Button3() MsgBox( 0, "", "Button 3" ) EndFunc Func Button4() MsgBox( 0, "", "Button 4" ) EndFuncWith a Map function to map events to functions, this could be even more useful. Then it could be used to handle events in message handlers for windows messages (message handlers created with GUIRegisterMsg). Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions 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