It seems like overkill to set the value of a nullable type and implement iNotifyPropertyChanged. Is there a better way of doing this?
Private _WorkPhone As Long?
Public Property [WorkPhone]() As Long?
Get
Return _WorkPhone
End Get
Set(ByVal value As Long?)
If value.HasValue = False Then
If _WorkPhone.HasValue = True Then
MyBase.RaisePropertyChanging("WorkPhone")
_WorkPhone = Nothing
MyBase.MarkDirty()
MyBase.RaisePropertyChanged("WorkPhone")
End If
Else
If _WorkPhone.HasValue Then
If _WorkPhone.Value <> value.Value Then
MyBase.RaisePropertyChanging("WorkPhone")
_WorkPhone = value
MyBase.MarkDirty()
MyBase.RaisePropertyChanged("WorkPhone")
End If
Else
MyBase.RaisePropertyChanging("WorkPhone")
_WorkPhone = value
MyBase.MarkDirty()
End If
MyBase.RaisePropertyChanged("WorkPhone")
End If
End Set
End Property
I tried using simple code, but my breakpoint on MyBase.RaisePropertyChanging(“WorkPhone”) is never hit, and the value never changes.
If _WorkPhone <> value Then
MyBase.RaisePropertyChanging("WorkPhone")
_WorkPhone = value
MyBase.MarkDirty()
MyBase.RaisePropertyChanged("WorkPhone")
End If
There is no need for all the complicated logic. If
xandyare both nullables with the same underlying type thenxequalsyif and only ifx.HasValueistruey.HasValueistruex.Valueequalsy.Valueor
x.HasValueisfalsey.HasValueisfalseIn neither of these cases would we want to raise a property changed notification and thus a simple test for non-equality will suffice. Thus:
Note that we ned to use
Not Nullable(Of T).Equalsinstead of<>as the latter evaluates toNothingif one if its operands isNothing.