Variable doesn’t exist in current context
I've got a rather odd situation which is bound to something breathtakingly obvious, but it has me stum开发者_如何学Pythonped.
Quite simply, I'm trying to load some configuration data into my app, as follows:
namespace XMLGrabberCS
{
class Grabber
{
public static void Main()
{
string test = "test";
string serverName = ConfigurationManager.AppSettings["ServerName"];
try
{
//do stuff...
Pretty straightforward stuff, except that if you query (i.e. via Quick Watch) either variable, you get a variable doesn’t exist in current context
error.
If you watch ConfigurationManager.AppSettings["ServerName"]
it does return the right value...
What on earth is going on here?
Are the strings referenced later on in the code? If not they could be being optimised away (unlikely if you're in debug, but worth a check).
Where are you when you try to use this in Quick Watch? If you're not currently debugging the Main
method, then it's absolutely right - that variable doesn't exist.
In the Stack Trace view you can set which level of the stack you're interested in. If you double click on the stack frame with the "Main" method, it should show the variables with no problem. Unfortunately, you can't do this while the Quick Watch window is open, as far as I can tell.
Did the debugger stop in the context when you watched for the variable ?
As ConfigurationManager
is global to the application it will work whatever the context is, so it is not surprising.
精彩评论