MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

VS cannot show information on variable

I have some arbitrary code. e.g. the following:

class MyClass
{
    private MyClass() { }
    public static readonly MyClass Instance = new MyClass();
    public Hashtable DoSomething() {return new Hashtable {{"key", "value"}};}
}

var test = MyClass.Instance.DoSomething();

Now when debugging and hovering test intellisense doesn´t show anything at all. Also adding a watch to the variable does not work. Instead the message

The name 'test' does not exist in the current context

appears. I already rebuilt the solution, closed VS and re-opened it. However when NOT debugging I get type-information on that variable within intellisense.

NB: Unfortunetaly the code above works within my test-solution, however the actual code which is far more complex does not. I already tried to simplify this as much as I can, supposing some downvotes as the error is hardly to reproduce. However maybe anyone has had a similar problem on VS.

EDIT: Optimization of code is disabled within projects settings (Properties-->Build-->optimize code)

Upvotes: 0

Views: 40

Answers (1)

thumbmunkeys
thumbmunkeys

Reputation: 20764

The compiler most likely optimizes the variable away because it is never used locally.

Use the variable in any way to circumvent this:

     var test = MyClass.Instance.DoSomething();
     Debug.WriteLine(test); // <=== Set breakpoint here

I'm not 100% sure, but I think optimizations also affect this. So to be sure, turn them off if you have these problems.

Upvotes: 1

Related Questions