Jump to content

Dim - Explination


ligenza
 Share

Recommended Posts

if that variable is used outside a local scope, it becomes a global variable and takes/changes the value assigned when declared in the global scope, it will not be reinitialised

Edited by Shibuya

The speed of sound is defined by the distance from door to computer divided by the time interval needed to close the media player and pull up your pants when your mom shouts "OH MY GOD WHAT ARE YOU DOING!!!"

Link to comment
Share on other sites

Easiest explained by code:

Global $variable = "Foo"
MsgBox(0, "", "Printfs 'Foo'=" & $variable)
function()
MsgBox(0, "", "Printfs 'Bar'=" & $variable)

Func function()
    Dim $variable = "Bar"; This is NOT a new local variable
EndFunc

Compared to this which uses explicit scope qualification (Local/Global can be used to dim arrays, too):

Global $variable = "Foo"
MsgBox(0, "", "Printfs 'Foo'=" & $variable)
function()
MsgBox(0, "", "Printfs 'Foo'=" & $variable)

Func function()
    Local $variable = "Bar"; Hide the variable at global scope
    MsgBox(0, "", "Printfs 'Bar'=" & $variable)
EndFunc
Link to comment
Share on other sites

Easiest explained by code:

Global $variable = "Foo"
MsgBox(0, "", "Printfs 'Foo'=" & $variable)
function()
MsgBox(0, "", "Printfs 'Bar'=" & $variable)

Func function()
    Dim $variable = "Bar"; This is NOT a new local variable
EndFunc

Compared to this which uses explicit scope qualification (Local/Global can be used to dim arrays, too):

Global $variable = "Foo"
MsgBox(0, "", "Printfs 'Foo'=" & $variable)
function()
MsgBox(0, "", "Printfs 'Foo'=" & $variable)

Func function()
    Local $variable = "Bar"; Hide the variable at global scope
    MsgBox(0, "", "Printfs 'Bar'=" & $variable)
EndFunc
Thanks.

Can't I just do this and get the same result? I'm not seeing the need for Dim.

Global $variable = "Foo"
MsgBox(0, "", "Printfs 'Foo'=" & $variable)
function()
MsgBox(0, "", "Printfs 'Bar'=" & $variable)

Func function()
;Dim $variable = "Bar"; This is NOT a new local variable
        $variable = "Bar"
EndFunc
Edited by ligenza
Link to comment
Share on other sites

So it is a local unless used as a global? ;)

if the variable appears anywhere out of the local scope, it becomes a global variable

take this for example:

Func Test1()

Dim $var = "Tesing";this variable is declared inside a function, which is supposed to be local
Msgbox(0, "", $var)

EndFunc

Func Test2()

Msgbox(0, "", $var); now the variable is being used outside Test1(), becomes global

EndFunc

so when Test2() is called, the output will be "Testing", even the variable is not declared and assigned a value inside the function

The speed of sound is defined by the distance from door to computer divided by the time interval needed to close the media player and pull up your pants when your mom shouts "OH MY GOD WHAT ARE YOU DOING!!!"

Link to comment
Share on other sites

Yes, your example is the same. However, Dim predates Global/Local and as such it has some... weird scoping rules. IMO, Dim should be deprecated in favor of the explicit qualifiers Local/Global but I've never pushed for this.

Link to comment
Share on other sites

i suppose it's convenient?

i've never used "global" or "local" so far, only dim

The speed of sound is defined by the distance from door to computer divided by the time interval needed to close the media player and pull up your pants when your mom shouts "OH MY GOD WHAT ARE YOU DOING!!!"

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