Global variable resets to zero ???

CedarTree

Registered User.
Local time
Today, 00:18
Joined
Mar 2, 2018
Messages
461
Hello - I use globals a lot, esp for a simple project. I set a watch on a global variable for any changes. At the end of one subroutine, the variable = 1000. And the watch flags it for going from the initial zero (I initialize to 0) to 1000. I step through and as soon as we move from that subroutine back to the parent subroutine that called it, the variable gets reset to zero. AND the watch doesn't even catch the change. I've tried recompiling, compacting. Why the reset? Any suggestions please?
 
can you post the code and indicate where it gets reset?
 
Proprietary coding so hard to share. Just never saw this before!!!
 
Where is the "Global" variable declared? In a general module or a class module?
 
Not sure if this helps but try declaring the variable as Static.
 
I think I found the issue. I had already declared it as a local variable as well. I would have thought the compiler would have caught that!
 
Last edited:
Why did I do that (b/c I'm an idiot) or why should the compiler have caught it (b/c you are declaring something redundantly and so a least a warning should apply).
 
I would have thought the compiler would have caught that!
Maybe it did but just didn't tell you because I think duplicate names are allowed as long as they are in separate modules. I could be wrong though...
 
My thoughts weŕè scope, where local precedes global.
Another reason to give descriptive names?
 
I think I found the issue. I had already declared it as a local variable as well. I would have thought the compiler would have caught that!
Bit like asking the compiler to ignore the rules it was set with? :)
 
This is a scoping problem. When you have the same name declared in a class module and a general module and you open the form for that particular class module, the variable in the class module is considered local (i.e. closer to) the code that references it. Access - like most languages I know using the "separate module" concept - uses a "scope" or "visibility" rule that says "closest reference wins."

If you are in a class module OTHER than the one that contains the declaration of that variable, the more distant reference wins. Note, however, that there is such a thing as a "friend" declaration for a variable that allows you to qualify your reference to an explicit class module and reference it from another place.

As many of my colleagues have noted, when you have duplicate names (and don't qualify the reference), you get the closest one, which is usually the local definition. So the variable that got updated was in scope when you set it to something other than zero, but when the subroutine exited, it went out of scope and the public variable hadn't been modified away from zero, so that's what you saw.
 
I am not, as many are, totally opposed to Globals. But with the advent of TempVars, tempvars do seem like a better option.
However, I like the explicit typing of globals, and people play awfully fast and loose with temp vars, which I don't like.
Six to one...
 

Users who are viewing this thread

Back
Top Bottom