0

I'm working on an existing class that is two steps derived from System.Windows.Forms.Combo box.

The class overrides the Text property thus:

    public override string Text
    {
        get
        {
            return this.AccessibilityObject.Value;
        }
        set
        {
            if (base.Text != value)
            {
                base.Text = value;
            }
        }
    }

The reason given for that "get" is this MS bug: http://support.microsoft.com/kb/814346

However, I'm more interested in the fact that the "if" doesn't work.

There are times where "base.Text != value" is true and yet pressing F10 steps straight to the closing } of the "set" and the Text property is not changed.

I've seen this both by just checking values in the debugger, and putting a conditional breakpoint on that only breaks when the "if" statement's predicate is true.

How on earth can "if" go wrong?

The class between this and ComboBox doesn't touch the Text property. The bug above shouldn't really be affecting anything - it says it's fixed in VS2005. Is the debugger showing different values than the program itself sees?

Update

I think I've found what is happening here.

The debugger is reporting value incorrectly (including evaluating conditional breakpoints incorrectly). To see this, try the following pair of classes:

class MyBase
{
    virtual public string Text
    {
        get
        {
            return "BaseText";
        }
    }
}

class MyDerived : MyBase
{
    public override string Text
    {
        get
        {
            string test = base.Text;
            return "DerivedText";
        }
    }
}

Put a breakpoint on the last return statement, then run the code and access that property.

In my VS2005, hovering over base.Text gives the value "DerivedText", but the variable test has been correctly set to "BaseText".

So, new question: why does the debugger not handle base properly, and how can I get it to?

3
  • Can you provide a simple, self-contained program that demonstrates the problem? Does this happen only on optimized builds or in debug builds as well? What versions of VS and .NET runtime are you using? Apr 16, 2010 at 15:28
  • This is in VS2005 using .NET 2.0. It's happening in debug builds. I'll strip the program down to the basics later and post it if it still happens.
    – Rawling
    Apr 19, 2010 at 7:52
  • Unfortunately this doesn't happen if I copy the class into a new project, so I doubt I'll get an answer related to my specific class :) However, if anyone has pointers for when the values seen in the debugger don't seem to match those seen by the program, they;d be welcome.
    – Rawling
    Apr 19, 2010 at 8:22

2 Answers 2

3

Use String.Compare for comparing strings. There are subtleties with strings. I cannot tell you why the if would fail, other than that your strings might not really be 'equal'

1
  • Same results using String.Compare - the string values in the debugger look the same, and String.Compare evaluated in the debugger can return non-zero, but an if statement (if (0 != System.String.Compare(base.Text, value) )) doesn't seem to work properly.
    – Rawling
    Apr 19, 2010 at 8:23
0

... and this just about wraps up my new question. Ah well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.