简体   繁体   中英

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 stumped.

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 (ie 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?

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.

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM