views:

202

answers:

2

I have code like this:

    private Box mCurBox;

    public Box CurBox
    {
        get { return mCurBox; }
        set
        {
            if (mCurBox != value)
            {
                mCurBox = value;
            }
        }
    }

When mCurBox is null then CurBox the debugger says "Could not be evaluated". If it knows that the value underneath is null then how come it can't figure it out?

+5  A: 

That's because you have not defined mCurBox to be anything by default, so the compiler flags this as undefined behaviour.
You need to initialize mCurBox as null, either in the very same line you define it, or in a constructor.
In general, it's good practice to initalize reference types to null if you don't assign something to them when defining them.
Also, seeing as you're just assigning and retrieving, you can easily use auto properties.

Rubys
+1  A: 

The debugger can get gimpy now and then. But the expected case of getting "Could not be evaluated" is a Release build. Simple properties like this get optimized away by the JIT compiler. The property getter code would not even be present.

Hans Passant