0

Is there a way to detect changes in a field or property without using INotifyPropertyChanged?

My point is to connect, for example, to some field and detect that its value has changed, even if it was an int field.

3
  • 1
    If you cannot control the way the field/property can be set, polling is your only option. Commented Dec 17, 2018 at 6:40
  • Of course, I do not want to check the change at a specific time, but rather to make the detection "automatic". Commented Dec 17, 2018 at 6:44
  • Polling means that you check the value repeatedly (e.g. using a timer), not at a specific time. Commented Dec 17, 2018 at 6:59

2 Answers 2

3

There is no effect you could use while writing an integer other than the one you wrote yourself. There is no way around INotifyPropertyChanged.

You can wrap the field with a property that raises the event. You can have a manager class that exposes a property and through which you manage that field even when the field is not part of that class. You could poll the value with a thread and if it changes raise the event.

The sane approach in most cases is to follow the mvvm blueprint as close as possible so no one is too surprised down the road.

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

Comments

2

You can decide to throw a customized event when a property changed. The only thing your upper layers have to do is subscribe to the SomeProperty event. You can replace the EventHandler delegate to something that suits your needs.

 public event EventHandler SomePropertychanged; private int _SomeProperty; public int SomeProperty { get { return this._SomeProperty; } set { if (this._SomeProperty != value) SomePropertychanged?.Invoke(this, new EventArgs()); } } 

5 Comments

How is this different from implementing INotifyPropertyChanged (except for the different event name)?
What does it change in relation to INotifyPropertyChanged? The operating scheme is identical.
I was hoping that it could somehow be detected at the level of Reflection or something like that.
@g_m You should update your question so that it reflects your hope. As it stands, I thought you just wanted to avoid INotifyPropertyChanged for some reason, so this seemed like a good and simple solution.
beautiful and simple solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.