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

  1. x.hasvalue true
  2. y.hasvalue true
  3. x.value equals y.value

or

  1. x.hasvalue false
  2. 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

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -