c# - Nullable Types and properties with INotifyPropertyChanged -
it seems overkill set value of nullable type , implement inotifypropertychanged. there better way of doing this?
private _workphone long? public property [workphone]() long? return _workphone end set(byval value long?) if value.hasvalue = false if _workphone.hasvalue = true mybase.raisepropertychanging("workphone") _workphone = nothing mybase.markdirty() mybase.raisepropertychanged("workphone") end if else if _workphone.hasvalue if _workphone.value <> value.value 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, breakpoint on mybase.raisepropertychanging("workphone") never hit, , value never changes.
if _workphone <> value mybase.raisepropertychanging("workphone") _workphone = value mybase.markdirty() mybase.raisepropertychanged("workphone") end if
there no need complicated logic. if x
, y
both nullables same underlying type x
equals y
if , if
x.hasvalue
true
y.hasvalue
true
x.value
equalsy.value
or
x.hasvalue
false
y.hasvalue
false
in neither of these cases want raise property changed notification , simple test non-equality suffice. thus:
private _workphone long? public property [workphone]() long? return _workphone end set(byval value long?) if not _workphone.equals(value) mybase.raisepropertychanging("workphone") _workphone = value mybase.markdirty() mybase.raisepropertychanged("workphone") endif end set end property
note ned use not nullable(of t).equals
instead of <>
latter evaluates nothing
if 1 if operands nothing
.
Comments
Post a Comment