473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

Jul 11 '08 #1
2 1504
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.
Jul 11 '08 #2
On Jul 11, 6:10*am, "C. Herzog" <cher...@web.dewrote:
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/N...ools/CodeRush/

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Jul 11 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steve Amey | last post by:
Hi all I am creating a basic control to perform some tasks, and I want to declare some events to be raised so they can be handled from the form that the control is on. I can create my own Event...
4
by: Irena | last post by:
Hi all again, Maybe, I'd better try to do this: How can it programmatically get/return the "Row source" or Rowsource property of a generic Table in a generic Access 2K database??? I found...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
45
by: Brett | last post by:
If I do this without declaring a corresponding field, is it considered bad design? What are the advantages or disadvantages to either method? Notice there is not set. public string URL { get...
6
by: bryanhobson | last post by:
I'm fairly new to c#, and I am just trying to work out how a 'properties' dialog works. Currently in my code, I have an object represented by the class 'Dog'. The dog object has several...
2
by: Marty McDonald | last post by:
When setting certain datagrid properties with the IDE (as in the property window), they don't seem to take effect. For instance, Font Name. So I have to set these via code-behind at the cell...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
17
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.