Jump to content

Dim, Local, Global


Recommended Posts

Dim = Local scope if the variable name doesn't already exist globally (in which case it reuses the global variable!)

Global = Forces creation of the variable in the Global scope

Local = Forces creation of the variable in the Local/Function scope

So I read that from the AutoIt help file.

But I really don't understand it.. that well.

In visual basic: Public can be used from any modules, and even in the form's code.

Dim can only be used where it is dimmed, so if there was a sub it could be dimmed there, and only used there.

So can anyone explain what the relationships are?

I'm assuming already Global = Public, Dim = Dim.

So what does Local do?

Link to comment
Share on other sites

Global creates a variable that can be read from anywhere in the script (thus "Global")

Local creates the variable within the function that it is called only. So within 2 different functions you can have $var = 2 different things.

Dim will create a variable in Local scope, unless that variable already exists in the Global scope, in which case, it will simply modify the global one.

Link to comment
Share on other sites

  • 4 months later...

More than anything I wish we had a Private Scope for UDF's

I want to declare a variable Globaly in an external script that is inaccessible in the calling script.

;## Included UDF
;   UDF_Secret.au3

#include-once

Local   $Local  = 'Local Secret'
Global  $Global = 'Public Secret'
Dim     $Dim    = 'Dim Secret'

Func _UDF_Func()
    
    ConsoleWrite('@@ UDF : $Local = ' & $Local & @crlf)
    ConsoleWrite('@@ UDF : $Global = ' & $Global & @crlf)
    ConsoleWrite('@@ UDF : $Dim = ' & $Dim & @crlf)
EndFuncoÝ÷ Ù«­¢+ØìáÕÑMÉ¥ÁÐ((¥¹±ÕÅÕ½ÐíU}MÉйÔÌÅÕ½Ðì()}U}Õ¹ ¤()1½°$ÀÌØí1½°$ôÌäí!¥©­Ìäì)±½°$ÀÌØí±½°ôÌäí!¥©­Ìäì)¥´$ÀÌØí¥´$ôÌäí!¥©­Ìäì()
½¹Í½±]É¥Ñ ÌäíM
I%APèÀÌØí1½°ôÌäìµÀìÀÌØí1½°µÀìɱ¤)
½¹Í½±]É¥Ñ ÌäíM
I%APèÀÌØí±½°ôÌäìµÀìÀÌØí±½°µÀìɱ¤)
½¹Í½±]É¥Ñ ÌäíM
I%APèÀÌØí¥´ôÌäìµÀìÀÌØí¥´µÀìɱ¤()}U}Õ¹ ¤

Currently the output is:

@@ UDF : $Local = Local Secret
@@ UDF : $Global = Public Secret
@@ UDF : $Dim = Dim Secret
@@ SCRIPT : $Local = Hyjacked
@@ SCRIPT : $Global = Hijacked
@@ SCRIPT : $Dim = Hijacked
@@ UDF : $Local = Hijacked
@@ UDF : $Global = Hijacked
@@ UDF : $Dim = Hijacked

But I want it to be:

@@ UDF : $Local = Local Secret
@@ UDF : $Global = Public Secret
@@ UDF : $Dim = Dim Secret
@@ SCRIPT : $Local = Hijacked
@@ SCRIPT : $Global = Hijacked
@@ SCRIPT : $Dim = Hijacked
@@ UDF : $Local = Local Secret
@@ UDF : $Global = Hijacked
@@ UDF : $Dim = Hijacked

:)

Please Executed scripts.... stop Hijacking my secrets!

--- TTFN

Link to comment
Share on other sites

More than anything I wish we had a Private Scope for UDF's

I want to declare a variable Globaly in an external script that is inaccessible in the calling script.

Currently the output is:

@@ UDF : $Local = Local Secret
@@ UDF : $Global = Public Secret
@@ UDF : $Dim = Dim Secret
@@ SCRIPT : $Local = Hyjacked
@@ SCRIPT : $Global = Hijacked
@@ SCRIPT : $Dim = Hijacked
@@ UDF : $Local = Hijacked
@@ UDF : $Global = Hijacked
@@ UDF : $Dim = Hijacked

But I want it to be:

@@ UDF : $Local = Local Secret
@@ UDF : $Global = Public Secret
@@ UDF : $Dim = Dim Secret
@@ SCRIPT : $Local = Hijacked
@@ SCRIPT : $Global = Hijacked
@@ SCRIPT : $Dim = Hijacked
@@ UDF : $Local = Local Secret
@@ UDF : $Global = Hijacked
@@ UDF : $Dim = Hijacked

:)

Please Executed scripts.... stop Hijacking my secrets!

Local/Global only differ when used inside a function. Declaring a variable with Local outside of a function still gets you a Global. Dim will assign Local/Global by context. If it is used inside a function, you get Local, if used outside a function you get Global.

To get the effect you want, declare $Local inside the function in the include file, but outside the function in the main script.

:P

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Local/Global only differ when used inside a function. Declaring a variable with Local outside of a function still gets you a Global. Dim will assign Local/Global by context. If it is used inside a function, you get Local, if used outside a function you get Global.

To get the effect you want, declare $Local inside the function in the include file, but outside the function in the main script.

:)

The effect I want is a Variable that is accessible from any function within the UDF file but is NOT accessible from the Calling script.

Per your suggestion you would have me do:

;## Included UDF
;   UDF_Secret.au3

#include-once

_SetVariables()

Func _SetVariables()
    Local   $Local  = 'Local Secret'
    Global  $Global = 'Public Secret'
    Dim     $Dim    = 'Dim Secret'
EndFunc

Func _UDF_Func()
    ConsoleWrite('@@ UDF : $Local = ' & $Local & @crlf)
    ConsoleWrite('@@ UDF : $Global = ' & $Global & @crlf)
    ConsoleWrite('@@ UDF : $Dim = ' & $Dim & @crlf)
EndFuncoÝ÷ Ùhbr"WºÇ­ç(¥z»h²°~íìqéâ¶'¬NººÞÚkiËbq謶¬y©ènÇ.­éÚ)àrïz{b¢v§vÚÊz'zíê-êâÉnuë(­Æ¥)à±Êâ¦Û.±ëazƦzö«¦åzv¦zÄáºËazW¬²X¤zÁ©í#ëö­{ñ¥¡¶¥½ªâi¹^²Úy§×§µ©eɧzËvØ^ÞºwëéÝ"!j÷­¢f¤zl¡×|¬yÖPí«p¢é]mçÚ¹r¡»n±ç§¢è!jëh×6; Originally $Local
     Local $GUID_5da07abe_4ec1_453e_bf1f_eb9dd9d14ad5 = 'Security through Obscurity is no Security'

I want my Globally/Private UDF Variables!

--- TTFN

Link to comment
Share on other sites

The effect I want is a Variable that is accessible from any function within the UDF file but is NOT accessible from the Calling script.

No. You can't have it. It just doesn't work that way. :P

At compile time the contents of the included file are actually copied in to the script. You see what that means? There is only one long script which contains all the code of your "main" script and every included file (and every file included by the includes, by the way). It all gets assembled into a single text file before it is processed. There is no separation between main and included code.

There is no security intended or provided by the variable scopes.

Avoiding naming collisions is easy, you just prefix the variable name with an identifier of the UDF. For example $UDF_One_MyVar in UDF_One.au3 and $UDF_Two_MyVar in UDF_Two.au3.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

File local variables don't exist yet, if they ever will.

More specifically, "File Local" makes no sense in AutoIt because at compile time the source is assembled into a SINGLE TEXT FILE before it is interpreted.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • 3 months later...

Basically..

.. Declaring a $variable as 'Local' or 'Dim' outside of a 'Funcion' is the same as declaring it as a 'Global'?

.. Declaring a $variable as 'Global' inside a function is a big no-no, as it can lead to bugs?, is non standard? (tidy has obvious complaints about this.)

You should instead use 'Dim' to redeclare a 'Global' variable.

That said, Using 'Dim' If the variable exists it will be simply overwritten with new data, if it does not already exist the variable will be created in a 'Local' scope?

So using 'Local' variable inside of a Function will overwrite a Global $variable with the same name?

I about as confused as these guys. :) I'll have to try some tests.

Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
Link to comment
Share on other sites

Before even thinking about this I was using 'Dim' for throw away/temp variables, Local for variables I'll have use for and Global for important variables, for my main script.

I guess some rewritting is needed :)

Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
Link to comment
Share on other sites

Before even thinking about this I was using 'Dim' for throw away/temp variables, Local for variables I'll have use for and Global for important variables, for my main script.

I guess some rewritting is needed :)

From the help file:

You should use Local or Global, instead of Dim, to explicitly state which scope is desired for a variable/constant/array.

Don't be Dim...

>_<

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I'm actually wondering here. If global isn't meant for inside functions, and variables at file level are by definition global, why is there a global keyword?

So that you can use it to create global variables from within functions.

Just because it is bad practice doesn't make it illegal...

However, I think it would make more sense if Local at file level actually made a variable that wouldn't be modified by Dim

Example:

Global $Var = 1

MsgBox(0,"Before",$Var)
UDF()
MsgBox(0,"After",$Var)

Func UDF()
    Local $Var = 2
    MsgBox(0,"Inside",$Var)
EndFunc

Result (as expected) = 1, 2, 1

And, If I do this:

Global $Var = 1

MsgBox(0,"Before",$Var)
UDF()
MsgBox(0,"After",$Var)

Func UDF()
    Dim $Var = 2
    MsgBox(0,"Inside",$Var)
EndFunc

Result (Also as expected) = 1, 2, 2

But this:

Local $Var = 1

MsgBox(0,"Before",$Var)
UDF()
MsgBox(0,"After",$Var)

Func UDF()
    Dim $Var = 2
    MsgBox(0,"Inside",$Var)
EndFunc

Gives this Result = 1, 2, 2

When i think it would be more logical if it returned = 1, 2, 1

Because that would mean that "Local" on the file level is not the same as "Global." Because Dim should only modify a variable that already exists in the Global scale.

Not that i can find a practical solution where doing this is better, but just a thought...

Edited by Paulie
Link to comment
Share on other sites

So that you can use it to create global variables from within functions.

Just because it is bad practice doesn't make it illegal...

However, I think it would make more sense if Local at file level actually made a variable that wouldn't be modified by Dim

Example:

; ...

Not that i can find a practical solution where doing this is better, but just a thought...

Remember that one of the design principles of AutoIt is the small, tight interpreter. Every feature of fully compiled language (VB, C++, etc.) that was added to AutoIt would bloat the interpreter. To make this work, for example, every variable used would have to track what context it came from, and that context would have to be checked on every variable reference.

If a project is that big, and that modular, such that it really required that kind of thing... why would you be doing it in AutoIt?

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • 3 months later...

Global on the outside of a function

Global is equal to inside function
Local is equal to outside function
Dim is equal to inside function


Local on the outside of a function

Global is equal to inside function
Local is equal to outside function
Dim is equal to inside function


Dim on the outside of a function

Global is equal to inside function
Local is equal to outside function
Dim is equal to inside function

So I would totally disregard the Dim as a useful tool and keep with Global and Local. The thing that intrigues me is that Global shouldn't be used with a UDF because you can openly pass parameters around those not needing any kind of outside functions. You should only need the Local inside the function to carry out the task.

The thing you did was say #include, so it did before executing itself. Now take a look at the code then:

;## Executed Script

;## Included UDF
;   UDF_Secret.au3

#include-once

Local   $Local    = 'Local Secret'
Global  $Global = 'Public Secret'
Dim     $Dim    = 'Dim Secret'

Func _UDF_Func()
    
    ConsoleWrite('@@ UDF : $Local = ' & $Local & @crlf)
    ConsoleWrite('@@ UDF : $Global = ' & $Global & @crlf)
    ConsoleWrite('@@ UDF : $Dim = ' & $Dim & @crlf)
EndFunc

_UDF_Func()

Local   $Local    = 'Hijacked'
Global  $Global = 'Hijacked'
Dim     $Dim    = 'Hijacked'

ConsoleWrite('@@ SCRIPT : $Local = ' & $Local & @crlf)
ConsoleWrite('@@ SCRIPT : $Global = ' & $Global & @crlf)
ConsoleWrite('@@ SCRIPT : $Dim = ' & $Dim & @crlf)

_UDF_Func()oÝ÷ Ù8^*.ë¡×^|Ó~¡Æ¥

The only use I see for Dim something would be inside a function in a UDF and thats the only place. As a users can and should be able to change it back to the way he wants it but not in the UDF.

Sorry for bringing this back even though it is a old topic just wanted to say this stuff for future use

0x576520616C6C206469652C206C697665206C69666520617320696620796F75207765726520696E20746865206C617374207365636F6E642E

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...