0

I have a variable

double total = 25;

I want event() to happen whenever total changes.

How do I monitor and cause event() to happen when total changes? For example, when total goes from 1 to 2, event happens, then when total goes from 2 to 3, event gets triggered again.

Edit. this is a variable, no I cannot make it into a property.

Thanks, I'm still learning.

babavic
  • 1
  • 2
  • Is total a field or a local variable? – ProgrammingLlama Jul 17 '20 at 00:02
  • 1
    If you can make Total a property, you can use this method to raise then event when the property changes: https://stackoverflow.com/questions/2246777/raise-an-event-whenever-a-propertys-value-changed – Pete -S- Jul 17 '20 at 00:04
  • 1
    This seems a little broad. Is this variable a class field or property? Does the class expose events already? What specifically are you stuck on? – Rufus L Jul 17 '20 at 00:09
  • http://www.shenchauhan.com/blog/2018/6/18/fody-inotifypropertychanged-the-easy-way – mjwills Jul 17 '20 at 00:11
  • total is a variable, it changes at different times – babavic Jul 17 '20 at 00:14
  • Check out reactive extensions: https://github.com/dotnet/reactive – TsTeaTime Jul 17 '20 at 00:16
  • It's a variable? OK, then the thing changing it will have to trigger the event. – ProgrammingLlama Jul 17 '20 at 00:17
  • Does this answer your question? [Raise an event whenever a property's value changed?](https://stackoverflow.com/questions/2246777/raise-an-event-whenever-a-propertys-value-changed) –  Jul 17 '20 at 00:35
  • I only have access to the variable. – babavic Jul 17 '20 at 00:41
  • 1
    @Olivier It's a local variable. – ProgrammingLlama Jul 17 '20 at 01:23
  • 2
    There isn't a magic bullet here. You can't just subscribe to changes on a local variable. You'll need to fire the events at the same time as you set the value. – ProgrammingLlama Jul 17 '20 at 01:23
  • 1
    `this is a variable, no I cannot make it into a property.` Create a new `Bob` class with a `double` property. Use `INotifyPropertyChanged` in the setter of the property. Change your local variable to be of type `Bob`. – mjwills Jul 17 '20 at 03:22
  • @babavic Impossible : you need to use an embedded value like mjwills said. You can create a generic for that, like `EmbeddedValue` with change and triggering events and all you need. –  Jul 17 '20 at 12:29

0 Answers0