The variable does not exist in the current context.

I have a rather strange situation that is connected with something incredibly obvious, but it puzzled me.

Simply put, I'm trying to load some configuration data into my application as follows:

namespace XMLGrabberCS { class Grabber { public static void Main() { string test = "test"; string serverName = ConfigurationManager.AppSettings["ServerName"]; try { //do stuff... 

Pretty simple stuff, except that if you request (i.e. via Quick Watch) any variable, you get the error variable doesn't exist in current context .

If you look at ConfigurationManager.AppSettings["ServerName"] , it returns the correct value ...

What's going on here?

+4
source share
3 answers

Listed below are the lines shown in the code? If not, they can be optimized (unlikely if you're debugging, but worth checking out).

+3
source

Where are you when you try to use this in Quick Watch? If you are not currently debugging the Main method, then it is absolutely right - this variable does not exist.

In the Stack Trace view, you can set what level of stack you are interested in. If you double-click on a stack frame using the Home method, it should show the variables without any problems. Unfortunately, you cannot do this while the Quick Watch window is open, as far as I can tell.

+3
source

Debugging stopped in context when you watched a variable?
Since the ConfigurationManager is global for the application, it will work regardless of context, so this is not surprising.

+1
source

All Articles