Jump to content

Answers to Exercises for AutoIt


Recommended Posts

11 minutes ago, TheSaint said:

Surely for beginners it is better to teach them to use Global at the top of their scripts.

Then later on, they can learn the more intricate benefits of Local.

I doubt it is better or worse. The distinction is not needed until you start writing functions. Using global variables will always have pitfalls regardless of the keyword used to declare them: the whole argument is swings and roundabouts. Reliance on global variables should be avoided if you want to learn to write good modular code (not only my opinion).

Link to comment
Share on other sites

I never said anything about reliance on Globals, but I believe in using the right declaration for the scope, and keeping it simple, especially for beginners.

Using Local at the top of as script in my view, is misleading to beginners.

And I don't see the issue with modular code.

Global or Local, each have their place, and a beginner is a beginner ... who is just getting started.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

What danger?

Coding is all about paying attention ... checks and balances. Declarations are no different to other aspects of coding.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

There is a danger of becoming reliant on the use of global variables, there is also a greater chance of corruption occurring due to accidental reassignment. There is potential confusion about why all the help file examples are inconsistent with forum posts; and finally, you can't just dump all your old code inside a function without having to redeclare every variable you ever created. I think it's a fair trade.

Edited by czardas
Link to comment
Share on other sites

If I remember correctly, isn't it impossible to declare Local variables outside functions? AutoIt automatically creates a global variable when declared out side a function regardless of the Keyword used to declare the variable.

Local $vGlobalVariable = "foobar"

Example()

Func Example()
    MsgBox(0, $vGlobalVariable, $vGlobalVariable)
EndFunc

That code snippet works for me :).

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

Years ago, coming from VB6, i used to declare most as Dim, then after a discussion by Valik and others, years ago now, I changed to Global.

I only used Local, when I really needed to.

That has served me well and still continues to do so, though I use Local a lot more often these days, after guinness's discussion about memory usage, which I took onboard.

That said, I have always found that Local declaration often requires much more thought than Global. Global is easy, you just need to keep tab on what you use and stay unique. It is not that hard. Like I said, you just need to pay attention, and the same can be said about all other aspects of coding.

EDIT
By pay attention, I mean be organized, structured and methodical in the way you do things. Don't create unnecessary extra work for yourself, making it harder to keep tabs on things or find them. There should always be a sensible method in your madness.

I for instance, always place my Globals at the start of my script, usually in two main sections (Controls, General), and unless it is a simple script, I never assign with a declaration. I keep things simple and alphabetical, which makes for quick checking.

I favor clarity, separation and readability over cleverish shortcuts with coding.

I rarely make a mistake with declarations, and in recent times it has been more with Local than Global.

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

41 minutes ago, iamtheky said:

Probably just a gist rather than a full repo, no?

nope, gists are hard to manage and they have limited version control. Also, gist isn't really suited for multiple files, hard to navigate and download the required bits link to the raw code or linking to specific lines of code in a file.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

ahh, so you have delusions of grandeur about how this will be maintained and used?  proceed.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Quite funny :lol:. I prefer to do it right when I do it, no matter how small "it" is  :D.

Edited by TheDcoder

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

23 minutes ago, TheDcoder said:

If I remember correctly, isn't it impossible to declare Local variables outside functions? AutoIt automatically creates a global variable when declared out side a function regardless of the Keyword used to declare the variable.

Local $vGlobalVariable = "foobar"

Example()

Func Example()
    MsgBox(0, $vGlobalVariable, $vGlobalVariable)
EndFunc

That code snippet works for me :).

And a good example too. Here we encounter no issues. You can also create a second (local) instance of $vGlobalVariable inside the function without encountering issues. You can't simply just use the Global keyword anywhere you want and remain within accepted norms. That's why I think that using Local gives beginners less to worry about: if anything using Global is the more complicated.

Link to comment
Share on other sites

1 minute ago, czardas said:

That's why I think that using Local gives beginners less to worry about

I think it would create more confusion for beginners :unsure:, why encourage them to use which is not the way it works just to make sure that they use Local inside functions?

The newbies use global variables, which are very easy to understand, as the newbies progress in their programming adventure, they will eventually recognize that Local variables are used inside functions... I see no problem here with using Global variables :).

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

Just now, czardas said:

It is a global variable already, but it shouldn't be: and that is the main point.

So are we talking about Local variables at the scope of "outside functions"?

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

It's all in the name.

Global means available to everything ... within or without.

Local means within only.

It's not rocket science, but for beginners at least, Local requires more thought.

Start simple and expand your horizons.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Just now, czardas said:

The variable is temporary and isn't needed more than once.

Something like this?:

Global Volatile $vOneTimeUseOnly

 

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Moderators

TheDcoder,

Volatile is only applicable to functions (as clearly stated in the Help file) and has no effect on variables.

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

Looks like you have misunderstood me Melba, I was trying to propose that as an idea, not to actually use Volatile to do what @czardas wanted :D

Edited by TheDcoder

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...