HowTo observe all Properties of a Class (and maybe cancel the try tochange the property)

C

C. Herzog

Hi!

I want to create a huge amount of classes. 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
Get
Return _Price
End Get
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.
Any Ideas?

My Idea was to use the reflection and observe the properties, but I
dont know how to do that.

Thanks for every comment.

C. Herzog
 
P

Phill W.

C. Herzog said:
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.
 
R

rowe_newsgroups

Hi!

I want to create a huge amount of classes. 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
    Get
      Return _Price
    End Get
    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.
Any Ideas?

My Idea was to use the reflection and observe the properties, but I
dont know how to do that.

Thanks for every comment.

C. Herzog

This won't be a fix it real quick solution, but if you constantly find
yourself writing the same type of code over and over again you might
want to check out one of the Visual Studio productivity tools. I've
been using CodeRush lately, and it's a great help to me (you can type
'pd Price' and get a fully qualified decimal property called Price).
I'm not sure if CodeRush has a template for the OnPropertyChanged
event models, but I haven't looked either. If it doesn't, its not a
huge effort to create a new template and add it to CodeRush that does
what you want.

Last I checked CodeRush was $249 per developer so I'd recommend you
download the trial and give it a whirl before committing as its not
for everyone. Also, I'm not associated with devexpress (the makers of
CodeRush), I'm just a fan of their software.

The link for CodeRush (it also include RefactorPro! in the price) is
below if you want to look at it or try out the 60 day version:

http://www.devexpress.com/Products/NET/IDETools/CodeRush/

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top