C# - Events Question - c#

Can any C# object be set up such that an event can be tied to it to fire when it's value changes? If so, how is this done? For example, let's take a very simple example. Say I have a variable declared as follows:
int i;
Is it possible to create an event that fires any time the value of i changes?
Thanks.

Well, you can't change fields so that events are fired when the value changes.
On the other hand, you can easily make it so that when a property changes, an event is fired. Only the author of the class containing the property can do this, however1 - you can't attach an event to an arbitrary existing class.
1 A slight exception would be a virtual property, which could be overridden solely for the purpose of raising the event. That would be pretty rare though.

Have a look at the INotifyPropertyChanged interface that can be implemented by a class and called by - at least some of - its properties to notify listeners that some values changed.

Another option would be to use the Observer pattern.
Grz, Kris.

Take a look at the PropertyChangedEvent pattern.

Related

When to use Event Properties in C# objects and when use just events

Would like to know the use-case wherein it is appropriate to use Event Properties instead of just Events and vice versa.
Well, obviously, event properties can be used to override the default behavior for adding/removing event handlers. For example if you want to make sure there's ever only one handler at a time, etc.
So my answer is: Unless there's something you don't like about the way event handlers are added/removed by default, keep your hands off event properties. If you have to do anything different from the defaults you'll have to implement event properties.
Personal opinion: In all the years I've been using C#/.NET now I have not once felt the need to add an event property...

Property Changed event within class

I got a question about events within classes.
I have a whole load of properties within my class and want to fire an event whenever a property is changed. I want to use the changed properties to calculate a result.
One possible option might be adding a call to every single property-set but this is - for my understanding - too q'n'd.
The common approach to realise this would be to implement the INotitfyPropertyChanged interface and raise the PropertyChanged event in every settter method for the properties where you need it.
Then you implement an event handler which reacts to the event and performs the operations needed.
https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

INotifyPropertyChanged with a property that's a class?

If I register to be notified of a property's change, it will only notify when the actual property is reassigned. So if I have a property that's a textbox, it won't notify if the text of the textbox changes, right? Is there any way to make it work this way?
If you are interested in specific properties of an object then you should be using a mechanism like INotifyPropertyChanged on the value itself. Unfortunately though in this case TextBox from WinForms doesn't implement INotifyPropertyChanged. In order to listen for changes to its Text property you need to subscribe to the TextChanged event
I have had a hard time imagining why a class would expose a property of type TextBox. Anyhow if you have ended up with such a design, you could simply add two properties, each for the TextBox itself and the text inside and then listen to TextBox's TextChanged event and fire your NotifyPropertyChanged thereupon.
I do feel however that you need to rethink your approach. If your class exposes a property of TextBox type, there should really be places where you actually assign a new TextBox to a class instance and upper layers listening to this change. I don't think that would be the case however. You'd almost certainly be looking for text changes and should therefore expose a property of type string.

Is there a predefined event to mark that an object has been invalidated?

I have an object which has properties. These are wired up with INotifyPropertyChanged. I also have collection properties which implement INotifyCollectionChanged. However, there is no event fired if an item already in a collection is altered.
I would like an event I can use to signal that there has somewhere been a change inside the object - is there one existing already? (it's fairly easy to do by hand, but more consitent if something already exists).
I know the System.Collections.ObjectModel namespace has an ObservableCollection that does this, is that what you're going for?

How to bind a WPF control value to a method?

Imagine I have a TextBox that it's Text should be equal to the number of running processes in the machine.
How to make it to update without using timers? Is there a way using Dependency Property or Bindings?
I'm not sure what this has to with binding to a method.
You'd have to keep calling the method on a timer, just the same.
You can set the DataContext to an object that implements INotifyPropertyChanged and exposes a property with the data you want (the getter can, of course, call a method).
Without a timer, you'll need an event to which your data object can subscribe, and then you can raise a notification that the property of interest has changed each time the event fires, and the UI element bound to that property will be updated.

Categories