Jump to content

Some big questions about au3 syntax ! Must see !


Recommended Posts

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

#2. Is this really really true that the first character after the $ sign in the variable name means the type of the variable ??? :shocked: :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 :shocked: ? 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
Endfunc

and not this instead :

#include <Misc.au3>

;Global declarations
;...

; the code

Will there be any problems whatsoever if the last variant ?

Edited by Rickname
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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 by Rickname
Link to comment
Share on other sites

  • Moderators

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 is

No, AutoIt does not "autofind" anything - it is just an aide-memoire for the coder. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
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
 Share

×
×
  • Create New...