Sign in to follow this  
Followers 0
Suja

Local,global,dim,

6 posts in this topic

i really don understand what there are for why not just use a single variable

1 person likes this

Share this post


Link to post
Share on other sites



Global and Local are types of scope. Global scope means that the variable declared will be able to be accessed and edited by every line in the script. Local scope means that the variable can only be used in the current scope (Funcs, for loops, etc).

The Dim keyword takes into account the scope of the variable before creating it. If it was not defined before, it becomes a local variable. If it was defined before, it re-uses the global variable.

These keywords aren't variables. Only something that starts with $ is a variable (for this purpose).

1 person likes this

Share this post


Link to post
Share on other sites

can you give me an example with using it in a script

1 person likes this

Share this post


Link to post
Share on other sites

I'm going to post a test script in a minute, but before I do I want to say this: Dim is really not necessary, and gets unnecessarily confusing if you don't know what you're doing.

1 person likes this

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Ok this might take some explaining, so I'm making it a new post.

Basically I just created a lot of variables in various scopes and added to them, and then tried to check them when I was done adding. The thing is, most of the "more stuff" message boxes are going to have Au3Check errors because the vars don't exist (the point of the exercise).

So... Global is global (used throughout the script) no matter where you put it (which is why you should always just put them at the top).

Local, when used in the main portion of the script, is the same as global. When used inside a function, it disappears when the function is ended.

Dim, when used in the main portion of the script, is also the same as global. When used inside a function on an existing variable, it is the same as simply modifying the variable (the same scope as the original is used). If it does not already exist, it is created with local scope, which means that it won't be able to be accessed outside the function.

For loops use local scope as well (in the first one, local is the same as global, because it is in the main - in the second one, it is local function scope).

Global $GlobalVar = 3, $DimThisVar = 9
Local $LocalVar1 = 6
Dim $DimVar = 7

For $i = 1 To 10
    MsgBox (0, "vars in loop " & $i, "GlobalVar = " & $GlobalVar & @CRLF & "LocalVar1 = " & $LocalVar1 & @CRLF & "DimVar = " & $DimVar)
    $GlobalVar += 1
    $LocalVar1 += 1
    $DimVar += 1
Next

MsgBox (0, "$i = " & $i, "GlobalVar = " & $GlobalVar & @CRLF & "LocalVar1 = " & $LocalVar1 & @CRLF & "DimVar = " & $DimVar)
TestFuncForVars()
MsgBox (0, "after func", "GlobalVar = " & $GlobalVar & @CRLF & "LocalVar1 = " & $LocalVar1 & @CRLF & "DimVar = " & $DimVar)
MsgBox (0, "more stuff 1", "j = " & $j)
MsgBox (0, "more stuff 2", "LocalVar2 = " & $LocalVar2)
MsgBox (0, "more stuff 3", "DimVar2 = " & $DimVar2)
MsgBox (0, "more stuff 4", "DimThisVar = " & $DimThisVar)

Func TestFuncForVars()
    Local $LocalVar2 = 1
    Dim $DimVar2 = 3
    Dim $DimThisVar = 1
    For $j = 1 To 10
        $GlobalVar += 1
        $LocalVar1 += 1
        $LocalVar2 += 1
        $DimVar += 1
        $DimVar2 += 1
        $DimThisVar += 1
    Next
EndFunc
Edited by greenmachine
1 person likes this

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Global is for outside functions which is visible from anywhere in the script.

Local is for inside functions which is visible within the function only and is destroyed when the function completes.

Dim is for inside functions which can be visible as either of the above depending on if Global scope is declared, else Local is used. This prevents a script from crashing and allows the function to continue with the knowledge that the value is empty and can be handled as such. Without Dim, then variables would need to use IsDeclared for each variable upon which several varaibles would be a pain in the butt.

Example of Scope shown

test0()             ; Global $var not Declared (Dim inside function avoids crash)
testisd()           ; Global $var not Declared (IsDeclared inside function avoids crash (As without Dim ???))
Global $var = 'Global'; Global $var Declared (Only Global is needed outside functions)
test1()             ; Global $var & Local $var Declared (Local used within function)
test2()             ; Global $var & Dim $var Declared (Dim used as Global as Global is Declared)
MsgBox(0, 'Global', '" ' & $var & ' "')

Func test0()
    Dim $var
    MsgBox(0, 'Dim (No Global)', 'Value: " ' & $var & ' "')
EndFunc

Func testisd()
    If Not IsDeclared('var') Then Local $var
    MsgBox(0, 'IsDelared (No Global)', 'Value: " ' & $var & ' "')
EndFunc

Func test1()
    Local $var = 'Local (Global Declared)'
    MsgBox(0, 'Local', 'Value: " ' & $var & ' "')
EndFunc

Func test2()
    Dim $var
    MsgBox(0, 'Dim (Global Declared)', 'Value: " ' & $var & ' "')
EndFunc

I use scope as mentioned above and find Dim as an important scope to make dynamically created scripts work correctly without error.

If you use Dim or Local outside functions then you are not using scope correctly.

Edit: ouch, changed incorrectly to correctly

Edited by MHz
1 person likes this

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