Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting property in Immediate Window not setting?

I have a problem in a project where I put a break point and start working with the properties of an object, and I can't set a value to a nullable int. I can set it to null, but if I set it to anything then immediately check the value, it returns 1.

I'm seeing this in the immediate window as well as the inspection window that pops up when you hover over a object when paused in debug mode. It also appears that this doesn't affect running code, just when you break and try to play with the values.

I was able to reproduce it in a new console project:

class Program
  {
    static void Main(string[] args)
    {
      var myValue = new TestItem()
      {
        NullableIntValue = null,
        StringValue = "My test value"
      };

      Console.WriteLine(string.Format("{0} is set to {1}", myValue.StringValue, myValue.NullableIntValue.HasValue ? myValue.NullableIntValue.Value.ToString() : "nothing"));

      myValue.NullableIntValue = 0;

      Console.WriteLine(string.Format("{0} is set to {1}", myValue.StringValue, myValue.NullableIntValue.Value));

      Console.ReadLine();

    }
  }

  public class TestItem
  {
    public int? NullableIntValue { get; set; }
    public string StringValue { get; set; }
  }

Paste that into a new console project, and put a break point at the "myValue.NullableIntValue = 0;" line. Then run it.

When the breakpoint hits, open the immediate window and set the value:

myValue.NullableIntValue = 123;
123

Then check the value:

?myValue.NullableIntValue
1

Try it with something else:

myValue.NullableIntValue = null
null
?myValue.NullableIntValue
null

Set it to anything other than null and it seems to always be 1.

You can also hover over the breakpoint line "myValue" and let VS pop open the inspection window where you see all the values. Go to NullableIntValue and set it to 100 (or anything) and hit enter and it immediately changes it to 1.

This only seems to behave this way for a property, i.e. if I change TestItem.NullableIntValue to a variable instead of a property it works fine. And if I assign a value to the NullableIntValue in code and let it run it seems to take it no problem, just not during a break in debug.

Note, also tried this in VS 2013 and it takes the assignment just fine, so it seems limited to VS 2015, and I tried both C# 6 and C# 5, happens on both.

Any ideas what could be causing this?

like image 864
Mike Avatar asked Sep 11 '15 15:09

Mike


People also ask

How do I enable immediate Windows?

To display the Immediate window, open a project for editing, and then choose Debug > Windows > Immediate or press Ctrl+Alt+I.

How do I display a variable value in immediate window?

Working With Variables To view the variable's value, simply type its name into the immediate window and press Enter. The value, "Hello world" will be displayed beneath the typed line, as in the image above. To access properties of a variable or value, use the member access operator (.) as you would within source code.

What is difference between immediate window and window?

Similar to the Command window, the Immediate window lets you test the code without having to run it. The Intermediate window is used to evaluate, execute statements, or print variable values. To open the Immediate window, navigate to Debug | Windows and select Immediate.


1 Answers

This is a known bug in Visual Studio 2015 RTM. We fixed it in Visual Studio Update 1. Essentially marshalling complex value types (like nullables) from the debugger to debuggee process was broken. If you don't want to install Update 1, you can work around it by going to Debug -> Options -> General and checking "Use the legacy C# and VB Expression Evaluators".

like image 136
Patrick Nelson - MSFT Avatar answered Nov 07 '22 03:11

Patrick Nelson - MSFT