C. Herzog wrote:
I want to create a huge amount of classes.
I don't, but I just find myself doing so ... :-)
All classes have in common that the change of a property should
fire an Change-Event and maybe do other things.
I don't want to write thousand times the same things like that:
Public Property Price() As Double
.. . .
Set(ByVal value As Double)
If Not _Price = value Then
Dim old As Object = _Price
_Price = value
RaiseEvent PropertyChanged(Me, New PropertyEventArgs("Price",
old, value))
End If
End Set
End Property
Writing this in every class (and every Property) makes no clear code.
Perhaps not, but that's roughly what you're going to have to do,
somewhere at least.
You /could/ create a CollectionBase-derived class and store all the
values of your class in that; IIRC, you can override a method on
CollectionBase that is fired whenever an item in the collection is
modified.
But you still have to write the code to do it.
If you only want one PropertyChanged event (rather than the more common
, one-event-per-property model, as used by Our Friends In Redmond) you
could have something like:
[Imports System.Reflection.MethodBase]
Public Property Price() As Double
Get
Return _Price
End Get
Set(ByVal value As Double)
If Not ( _Price = value ) Then
Me.OnPropertyChanged( GetCurrentMethod().Name _
, _Price, value )
_Price = value
End If
End Set
End Property
Protected Overridable Sub OnPropertyChanged( _
ByVal sName as String _
, ByVal oldValue as Double _
, ByVal newValue as Double _
)
Dim e as New PropertyChangedEventArgs( sName, oldValue, newValue )
Me.OnPropertyChanged( e )
End Sub
Protected Overridable Sub OnPropertyChanged( _
ByVal e as PropertyChangedEventArgs _
)
' Raise the PropertyChanged Event to registered Delegates
RaiseEvent PropertyChanged( Me, e )
End Sub
HTH,
Phill W.