Rickname Posted April 28, 2013 Posted April 28, 2013 (edited) Im relative new to this awesome script language and every day Im mindblowed but what things I discover and didn't know about the au3 syntax but I have some questions : #1. What is ByRef and what it does/why to use it/where to use it #2. Is this really really true that the first character after the $ sign in the variable name means the type of the variable ??? :shocked: ( Like $iVar = int and $sVar = char types from C ? )What about if I declare $Random instead ? If the above is true, then why all my created programs are working with random names without the correct specific type declared ? Like $pink = 5 + 5 does works despite the 'p' representing the pointer type !#3. Why I need to unregister things or close any handles like DllClose after a DllOpen OR _GDIPlus_Shutdown() after my script exists ?#4. And last but not the least why this :#include <Misc.au3> ;Global declarations ;... Func () Func() ; the code Endfuncand not this instead : #include <Misc.au3> ;Global declarations ;... ; the codeWill there be any problems whatsoever if the last variant ? Edited April 28, 2013 by Rickname
Moderators Melba23 Posted April 28, 2013 Moderators Posted April 28, 2013 Rickname,Here we go: ByRef: When you pass parameters to a function you can do it in 2 ways: - By value (the default) where the function makes a copy of the variable to use and destroys it at then end of the function leaving the original unchanged.- By reference (ByRefwhere the function is told where the original variable is located. Any changes to this original made by the function remain after the function ends.If you look at the Variables - using Global, Local and ByRef tutorial in the Wiki, you will see some examples showing this in action.Letter after the $: You can call your variables whatever you like (as long as you use alphanumeric characters and underscores) but as AutoIt is not a typed language, coders often use the first letter to indicate what type variable is being held in that particular name. My personal usage is:$a = Array $b = Binary $c = ControlID $f = Boolean flag (True/False) $h = Handle $i - Integer number $m = Menu item $n = Floating point number $p = Pointer $s = String %t = Struct $v = Undefinedbut you can obviously vary this according to you personal preference. You do not need to do it at all, but experience has shown that it is a good idea.Tidying up: Autoit is extremely well-behaved and does a lot of housekeeping after a script ends to make sure that most of the mess (such as open handles) is tidied away. However it behoves the careful coder to do as much clearing up as possible within the script - not only is this good practice, but it prepares you for when you move on to less-forgiving languages. In certain cases, such as when you use AutoIt to create very complex items like DLL callbacks and GDI handles, AutoIt will not correctly clear up when it is shut down and it is imperative that the coder tidies up the code. If not the system could become unstable or exhibit memory leaks.Functions: There are no problems with your second example, although there is with the first! Most of the Help file examples have the code within a function - this is done deliberately to reduce the number of Global variables, which is generally regarded as good coding practice. In longer more complex scripts you will certainly need functions - in small scripts you can often do without. Does that make it all clearer? Please ask if you have any more (or indeed any supplementary) questions. M23 Rickname 1 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
Rickname Posted April 28, 2013 Author Posted April 28, 2013 (edited) Rickname, Here we go: ByRef: When you pass parameters to a function you can do it in 2 ways: - By value (the default) where the function makes a copy of the variable to use and destroys it at then end of the function leaving the original unchanged. - By reference (ByRefwhere the function is told where the original variable is located. Any changes to this original made by the function remain after the function ends. If you look at the Variables - using Global, Local and ByRef tutorial in the Wiki, you will see some examples showing this in action. Letter after the $: You can call your variables whatever you like (as long as you use alphanumeric characters and underscores) but as AutoIt is not a typed language, coders often use the first letter to indicate what type variable is being held in that particular name. My personal usage is: $a = Array $b = Binary $c = ControlID $f = Boolean flag (True/False) $h = Handle $i - Integer number $m = Menu item $n = Floating point number $p = Pointer $s = String %t = Struct $v = Undefined but you can obviously vary this according to you personal preference. You do not need to do it at all, but experience has shown that it is a good idea. Tidying up: Autoit is extremely well-behaved and does a lot of housekeeping after a script ends to make sure that most of the mess (such as open handles) is tidied away. However it behoves the careful coder to do as much clearing up as possible within the script - not only is this good practice, but it prepares you for when you move on to less-forgiving languages. In certain cases, such as when you use AutoIt to create very complex items like DLL callbacks and GDI handles, AutoIt will not correctly clear up when it is shut down and it is imperative that the coder tidies up the code. If not the system could become unstable or exhibit memory leaks. Functions: There are no problems with your second example, although there is with the first! Most of the Help file examples have the code within a function - this is done deliberately to reduce the number of Global variables, which is generally regarded as good coding practice. In longer more complex scripts you will certainly need functions - in small scripts you can often do without. Does that make it all clearer? Please ask if you have any more (or indeed any supplementary) questions. M23 So awesome Melba ! Really awesome thanks alot ! You really explained everything the best and all my problems got resolved ! So ByRef is like the & variable address referance type like in Borland C => very helpful thing in AutoIt !! Letter after variable is not a must to show what type of data will be saved like you must declare the types in C, so this also awesome because AutoIt will auto-find what type of data is Tyding up : "If not the system could become unstable or exhibit memory leaks." -> now I understand why is so important Thanks alot ! Very helpful Edited April 28, 2013 by Rickname
Moderators Melba23 Posted April 28, 2013 Moderators Posted April 28, 2013 Rickname,When you reply, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - I know what I wrote and it just pads the thread unneccessarily. because AutoIt will auto-find what type of data isNo, AutoIt does not "autofind" anything - it is just an aide-memoire for the coder. M23 Rickname 1 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
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