I have a chunk of code where it appears that a variable is changing at the end of a pre-processor block of code.
int initialKeyCount; #if(DEBUG) // int initialKeyCount = _root.CountAllKeys(); initialKeyCount = 20000; #endif currNode = currNode.EnsureDegreeKeysPresent(parent); //initialKeyCount = 19969 here #if(DEBUG) int currentKeyCount = _root.CountAllKeys(); Debug.Assert(initialKeyCount == currentKeyCount, string.Format('EnsureDegreeNodesPresent changed the node count from {0} to {1}.', initialKeyCount, currentKeyCount)); #endif
When executing this in the debugger initialKeyCount = 19969 after supposedly assigning 20000. I have played around with this a bit and found that assignment to initialKeyCount is correct inside the first pre-processor block, but as soon as the code leaves the first pre-processor block the value magically changes to 19969.
This behavior is the same regardless of whether the variable is declared inside or outside the first pre-processor block. The value remains 19969 inside the second pre-processor block.
Are assignments made in a pre-processor block undefined outside of that block? That seems wrong but appears to be what is happening here.
Sounds like Visual Studio got confused. Try these steps in order
I used to see this once a week at an IT company I worked for. It usually happens when you have multiple copies of the same project, but I’ve seen it even without that.