Jump to content
Sign in to follow this  

Help with defining variables

Recommended Posts


OK, this is a very basic programming question, but one that I am unsure of. I never had much formal programming training (and what I did have was mostly with Fortran - Watfor, actually) back in the 19-mumble-mumble's. I guess I am looking for some free education.

From what I can understand, it is good practice to define your variables at the start of the program. This I understand and makes sense and, like flossing, it's a pain, but I try to do it since I know it will make my life better in the long run.

Defining a "Global" variable means that the variable will be available to the code where it was defined as well as any other code called by the original code. This can create problems when a sub-routine is called that may use a variable defined as Global by the original program. A good example with AutoIT is using $i as a counter. If defined as a Global variable, and a routine or function is called that uses the same variable can alter its value unintentionally, with unpredictable results. Global variables do not have to be passed to these sub-routines in the function call.

A "Local" variable is essentially the opposite, that is, it is defined only in the original script and for the value of that variable to be used in a sub-routine, the value must be passed in the function call.

What if a variable is defined in a "Dim" statement? Is it Local or Global?

My immediate issues is that I am setting up a script that involves creating and defining a number of IE objects (not game related, in fact, it's work related, pretty much anti-game). To simplify my code, I would like to create a function to set-up all of these objects at the beginning. As there are over 20 of these objects, I really don't want to have to individually pass these variables in the function call, so how do I take care of it?


Share this post

Link to post
Share on other sites

Don't use Dim because it can inadvertently re-declare a global variable if used in a function. ALWAYS use Global for variables used throughout the script, and Local for variables used inside the function it's declared in. Never use Global inside a function to declare a variable that is going to be used as a Global, declare them first then if you need to set a value to them in a function they're already declared. Doing it inside a function can lead to some tricky debugging being needed to fix an error with a variable that hasn't been declared yet.

ReDim is a special case and only used with Arrays.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post

Link to post
Share on other sites

You can also take a look here.

Br, FireFox.


OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control


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