Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Field Value not setting...

On this page:

Here’s a nice baffler that’s been giving me fits for about an hour now. Check out this debugger highlight:


I’m running into this situation where a field up the inheritance structure will simply not set. In this case the _IsEntityDirty field was just assigned a value of true, yet the debugger (and not just the debugger – conditional code actually sees this value as false!) sees the value as false.  Note this is a protected field defined 2 levels up the hierarchy.


I can’t figure out WTF is going on. Actually the field shuldn’t be accessible at this level – I have a public property for this but to simplify the debugging I actually made the field public so I can see the value change. With the property assignment I can step into the assignment and see the field value change to true, then when I step back out to the current level the value changes immediately back to false. No other code runs…


WTF is going on here?


The hierarchy is something like this:






IsEntityDirty is defined at the wwBusiness level as:


/// <summary>

/// internal flag used to tell when the entity needs to be refreshed.

/// </summary>

protected bool IsEntityDirty




        return this._IsEntityDirty;




        this._IsEntityDirty = value;



protected bool _IsEntityDirty = true;


(the protected _IsEntityDirty is temporary so I could test direct assignment up the hierarchy).


I even took out the property and just created a field:


protected bool IsEntityDirty = true;


and used that field and it still didn’t work. Added another field


protected bool IsEntityDirty2 = true;


Same identical signature right? And IT WORKS just fine.


I have several other protected and public properties that are defined identically, yet they all work fine. I can’t set the value in the debugger either.


Am I having a bad dream? Somebody pinch me… <s>


I’ve run into stuff like this before and in those situations it seems that the problem usually had to do with an out of date assembly where a minor change ended up causing weird inconsistencies like this. But I double checked all assembly references and ensured the right assemblies are getting referenced and compiled into the output directory. That’s not it this time…


… Later after endless fiddling …


It turns out the problem had something to do with the Solution file. I’m not clear on what the issue is exactly, but I stuck the component into another solution for testing and it worked fine. Added a new project to the existing ‘bad’ solution and regardless of project – console, web the code simply did not work exhibiting the above behavior.


So I created a brand new project and solution added the assembly and sure enough it worked. So there appears to be some configuration problem in the solution.


Looked at the existing solution and found:


Some remnant Test files – I had played around with the built in test generation in Visual Studio, but it was such a pain in the ass I switched back to using nUnit. However, even though I removed the VS test project it left behind a bunch of shit in the solution and in the folder structure. After clearing out those files and nuking the solution .suo file and starting the original solution back up – everything is back to normal and my code once again runs correctly.


I can’t figure out what in the world could have made for this behavior. I double checked the assemblies generated and the failure occurred even when running outside of Visual Studio. But even if it was only inside of VS.NET – what could possibly cause code to not be able to assign a value to a specific field?   The only thing I can think of is a version mismatch but I checked and double checked the assembly signatures and even double checked the GAC to ensure there wasn't a stray reference in some other location getting called.


I can’t believe this just cost me an hour and a half worth of time…

Posted in .NET  

The Voices of Reason


Ayende Rahien
February 07, 2007

# re: Field Value not setting...

The issue is such cases is usually the debugger auto-evaluating the properties, which means that some property is setting the field value back to false again.

Rick Strahl
February 07, 2007

# re: Field Value not setting...

But it's not a property - it's a field and there's no intermediate code. The code was not working at runtime either. In fact, inside of the debugger I could set up the field for display and I couldn't set the value of the field. Simply wouldn't assign...

Ayende Rahien
February 07, 2007

# re: Field Value not setting...

check that the debugger is not evaluating ANOTHER property that is causing this.
Check the locals / watches tabs to see if there is something there that might do this.
then close them and try again.

Rick Strahl
February 07, 2007

# re: Field Value not setting...

Hmmm... you have something there. I have this fixed now when I nuked the .suo file, so I don't know now what other stuff was in the debugger. From what I remember though there was nothing else active in the watch window.

Marc Brooks
February 07, 2007

# re: Field Value not setting...

How about you are looking at it with the option to suppress optimizations at JIT time turned OFF, so the value are not properly being evaluated by the debugger?

Hector Correa
February 08, 2007

# re: Field Value not setting...

I've seen a similar issue in which test projects don't pick up changes from the projects that they reference. I simple "rebuild all" at the test project level usually takes care of my problem but it's strange the way the program runs without any indication that it didn't pick up the changes during the compile process.

Rick Strahl
February 08, 2007

# re: Field Value not setting...

Thanks for all the info guys...

As Ayende mentioned it looks like the issue is definitely related to the debugger. It turns out that the if the hosting property in the code above (ie. this or the business object) is in the debugger watch window the field will not set. I don't understand why that is but the Entity itself sitting on the business object is firing a getter so I suspect something is happening there.

But what's odd is that it shouldn't ever change that value of this field. Also if I set breakpoints in the code get method of the this.Entity or this.IsEntityDirty they are firing except when I step through with code.

This is REALLY, REALLY bizarre behavior and frankly very unsettling. I can live with the debugger displaying the wrong thing (and hopefully I'll remember this episode when something similar happens in the future) but apparently the debugger actually affects the behavior of the object in code running with the debugger attached. It's not just that the debugger is displaying the wrong value, but the value is wrong in the executing code...

I'm glad at least that I know what's causing the issue at this point - I have seen this before and was really scratching my head before...

Rick Strahl's Web Log
June 23, 2007

# Rick Strahl's Web Log

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024