0

In the beginning, there was code:

public abstract class A
{
    private string _someValue;

    public string SomeValue
    {
        get
        {
            if (_someValue == null)
                _someValue = GetValue();
            return _someValue;
        }
    }

    protected virtual string GetValue() { /* logic */ }
}

public class B : A
{
    protected override string GetValue()
    {
        return base.GetValue() + GetMoreValue();
    }

    private string GetMoreValue() { /* logic */ }
}

And the code said, "Let there be bugs!" and there were bugs.


Seriously now.

I have an instance of B, and when I get SomeValue, I get the same SomeValue of A, without the MoreValue.

The even weirder part came when I put a breakpoint on SomeValue's Get method:
It turns out that _someValue gets its value before the Get method is ever called.

Something is very wrong here.


UPDATE:
Thanks for the comments! Shortened code, and added forgotten return type in B method.

Yehuda Shapira
  • 8,460
  • 5
  • 44
  • 66
  • 1
    Please post a [SSCCE](http://sscce.org/). – nvoigt Dec 17 '13 at 09:46
  • 4
    Make your code compile (B.GetValue() needs a return type; A.GetValue() and B.GetMoreValue() needs initialized return) then debug your example and you'll see that it works as expected (B.SomeValue contains both of your values). My bet is that the bug that you are hunting is somewhere in the left out code - so you already made a very important step to locate it. – Jan Dec 17 '13 at 09:49
  • I test it it works well, the `GetMoreValue` of B is called. Did I miss something? – Thomas Dec 17 '13 at 09:50
  • What does `GetMoreValue();` return? Set a breakpoint and check that it isn't an empty string - would give the appearance of `SomeValue` being the same as `A` – DGibbs Dec 17 '13 at 09:54
  • 3
    Are you debugging and have you configured the debugger to automatically retrieve property values? If the debugger automatically reads `SomeValue`, `_someValue` will get set. –  Dec 17 '13 at 09:57
  • @hvd How do I do that? – Yehuda Shapira Dec 17 '13 at 10:09
  • 1
    @JacobSpire That's the "Enable property evaluation and other implicit function calls" setting in VS, Tools, Options, Debugging, General. –  Dec 17 '13 at 10:39

1 Answers1

0

It turns out I was making a stupid mistake.
(I would delete the question, but I figured others can learn from my blooper.)

I had a watch on SomeValue.

The result? Get SomeValue was called the moment I started debugging.
(Which is a little weird, because I had a breakpoint on the Get method which didn't activate. For some reason, watches don't trigger breakpoints.)

*(As for the wrong value, that had nothing to do with inheritance -
GetMoreValue simply happened to return an empty string.)

Yehuda Shapira
  • 8,460
  • 5
  • 44
  • 66