Sign in to follow this  
Followers 0
junkew

functions in variable $myMsgbox=msgbox

5 posts in this topic

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

Share this post


Link to post
Share on other sites



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.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Also look in my signature for _GUIRegisterMsgEx().


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

Here is an example. Traditional code:

#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" )
EndFunc

A problem here is that the more controls you add, the more time will be used testing Case statements.

This problem is eliminated here:

#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" )
EndFunc

With 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).

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0