Plunging into .NET Development

Weblog Pieter Gheysens
Microsoft .NET Development - C# - Enterprise Library - Visual Studio 2005 Team System - Compuware DevPartner - ...
 


Wednesday, May 18

error : identifier 'xxx' out of scope

Ever been in the situation where the Command Window or ToolTip doesn't "function" properly while debugging (Visual Studio 2003)? Yip, quite annoying ... but it makes sense! See the example below ...



Why can't I get the counterString-value? I'm NOT out of scope!?

Solution

Check the build configuration properties of your project and make sure that the "Optimize Code" property for Debug is set to false! After that "correction" you will see that the variable will return its value ...





Explanation

When the compiler optimizes code, it repositions and reorganizes instructions, resulting in more efficient compiled code. Because of this rearrangement, the debugger cannot always identify the source code that corresponds to a set of instructions. For this reason, you should do your debugging using an unoptimized version of your program if at all possible.


Below you find two screenshots of the disassembled code (Reflector) : the first one with the "Optimized" option set to false and the second one with the "Optimized" option set to true.





In the optimized version you can verify that there's no variable that contains the counterString. As a matter of fact, it's normal/logical because that variable is never used in its scope ... but in certain circumstances (not in my example) these variables can contain valuable (temporarily) information while debugging! Make sure to set the "Optimize Code" property to false when you want to deal with those kind of things. The default setting is false for Debug and true for Release. No bug in Visual Studio after all, but you have to know it ;-)

1 Comments:

  • At 1:16 PM, Anonymous Anonymous said…

    I do have same problem, but in VS 2008 web site project. I couldn't find [optimize code] checkbox in VS 2008 :(

     

Post a Comment

<< Home