So I get a paniced IM from a coworker saying this if statement is skipping when
If true Then
DoSomething
End IF
The actual code being
If MyBase.IsDirty Then
DoSomething
End IF
C# Version
if(base.IsDirty)
{
DoSomething();
}
My first question to them was well the If
statement isn't magically broken so are you sure MyBase.IsDirty
is actually true? They responded 'Yes OF COURSE!'. Upon asking them how they determined this they said they were putting a breakpoint at the line and hovering over MyBase.IsDirty
to see what the value was and that they had also done ?MyBase.IsDirty
in the immediate window. My initial thought and what I am thinking to still be correct is due to the way that base classes function that it wasn't actually showing the value at in those cases (at least the correct one) so I suggested doing
Dim dirty As Boolean = MyBase.IsDirty
Then running the code again, which showed as I guessed that dirty was actually false
.
Now why is this the case? As far as I can tell it has something to do with the way that MyBase is implemented but I want specifics so I can have a 100% answer to this.
Edit
Right so MyBase.IsDirty
is a Boolean
The Get is done as Follows:
<Browsable(False)> _
Public Overridable ReadOnly Property IsDirty() As Boolean Implements IEditableBusinessObject.IsDirty
Get
Return IsSelfDirty OrElse (_fieldManager IsNot Nothing AndAlso FieldManager.IsDirty())
End Get
End Property
Where
<Browsable(False)> _
Public Overridable ReadOnly Property IsSelfDirty() As Boolean Implements IEditableBusinessObject.IsSelfDirty
Get
Return _isDirty
End Get
End Property
And
_isDirty
is Initialized as true
(Field Manager is a non issue for this case).
I should note that this is all taken from CSLA 3.5.3.0
Another Edit
I'm calling MyBase.IsDirty because that is the way that Rocky Lhotka
shows it being done in his books on CSLA.
The debugger can't evaluate expressions using the base
(or MyBase
in VB) keyword correctly because the debugging API doesn't support non virtual calls. It returns the value a virtual call to the getter returns instead of the one the getter in the base class returns. So the effect you're observing is a debugger problem.
I guess you overrode the property in the derived class and return false
there, and that's what the debugger shows. The compiled code itself will handle the base.
call correctly.
The MSDN blog entry "Debugger won't properly evaluate C#s base keyword" describes this problem in detail