Dim a Global in a sub

kickaha

Well-Known Member
Licensed User
Longtime User
Erel,

Whilst answering another question on here it occured to me that you can (re)Dim a global variable within a sub, this COULD lead to problems if you wanted a local and you trashed your global value.

I know that it is necessary to be able to do this, but could the IDE not issue a warning that you have done this at compilation, it would make debugging this issue much easier.
 

warwound

Expert
Licensed User
Longtime User
Wouldn't the global and 'local to a sub' variable be different variables even though they have the same name.

FYI
I know that's how scope works with javascript where the following applies:

So if you have global var 'a' whose value is 10 and a function with a local variable named 'a' whose value is 20, then if you get the value of variable 'a' within the function you get the value 20.

If you get the value of variable 'a' where no other local variable has been defined with the same name then you'd get the value 10.

That is a local variable takes precedence within a function over a global variable with the same name.
In fact in javascript the scope chain is searched for the variable and it's found first in the local scope - the search for the variable ends there.
The rest of the scope chain where the global variable exists is never searched.

Martin.
 

kickaha

Well-Known Member
Licensed User
Longtime User
I apologise for my bad English:sign0013:. Yes I do think a warning would be good.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…