Matthew Ives
Matthew Ives

Reputation: 41

Why are my VS2008 watches showing incorrect values?

I have a string variable and a string constant. Both should be the same value (I'm testing for equality in a conditional). The 'correct' values of both should be "scl". While debugging, if I put a watch on each, look at them in the 'locals' windows, or hover over them, the value displayed is "sd", which is the value of a different constant in the class (there are many other constants and variables in the class that are displaying values correctly). If I insert a Debug.WriteLine for the variable/constant value in question, (in the same scope as the watch) the output window prints the correct value of each. For the life of me, I can't figure out why this is happening, or how to correct it.

Upvotes: 0

Views: 286

Answers (2)

Matthew Ives
Matthew Ives

Reputation: 41

I seemed to have fixed it by changing the value of the constant, running a debugging session, then changing the value back to what it should be. Perhaps this cleared out some kind of debugging cache.

Thanks for the help all!

Upvotes: 1

PaulG
PaulG

Reputation: 14021

Is it a lazy loaded property? I've had issues like this in the past where I've done something like this (horribly contrived example, but it will do)

public ClassWithMoo
{
   private string moo;

   public string Moo
   {
      get
      { 
         if (String.IsNullOrEmpty(this.moo)) this.moo = "Baa";
         return this.moo; 
      }
      set
      {
         this.moo = value;
      }
   }
}

public ClassThatUsesMoo
{
    ClassWithMoo cow = new ClassWithMoo();

    // breakpoint here would show cow.Moo = "Baa" 
    // This is because the debugger/watch window will instantiate the property!

    someCodeHere();

    cow.Moo = "Moo"; 
    debug.WriteLine(cow.Moo); // outputs 'Moo' now that it has been set properly
}

Upvotes: 0

Related Questions