1

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.

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I need to take a typing class.
2

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.

1 Comment

+ Lots. INotifyPropertyChanged is the standard interface in .NET for this behavior, and it is nearly essential for databinding in WPF and Silverlight.
0

Another option would be to use the Observer pattern.

Grz, Kris.

Comments

0

Take a look at the PropertyChangedEvent pattern.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.