Sign in to follow this  
Followers 0
ligenza

Dim - Explination

8 posts in this topic

#1 ·  Posted (edited)

Nothing to see here, please move along.

Edited by ligenza

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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!!!"

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
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!!!"

Share this post


Link to post
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.

Share this post


Link to post
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!!!"

Share this post


Link to post
Share on other sites

i suppose it's convenient?

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

Yeah I suppose. Thanks for all the feedback. ;)

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