Is it possible to raise events only when the subscribed methods’ arguments fulfill a condition?

Hi all, I've made a simple class that contains a value that is automatically clamped to min and max values, called MinMaxInt. I would like to be able to create a child class that contains delegates and events and call them when the value reaches certain thresholds as dictated by the subscribed methods.

For example, I would like to be able to say:

public MinMaxInt health = new MinMaxInt(0, 5, 3); // Min, Max, Value health.OnValueAbove += SomeFunctionCalledWhenValueAbove(4); health.OnValueBelow += SomeFunctionCalledWhenValueBelow(2); 

This is what the child class looks like so far. Is there a way to use events as described above?

public class MinMaxEventInt : MinMaxInt { public delegate void EventHandler(); public delegate void EventHandlerInt(int value); public delegate void EventHandlerFloat(float value); public EventHandler OnValueChanged; public EventHandler OnValueMax; public EventHandler OnValueMin; public EventHandlerInt OnValueAbove; public EventHandlerInt OnValueBelow; public override int value { get { return _value; } set { if(this.value == value) return; _value = Mathf.Clamp(value, _min, _max); if(OnValueChanged != null) OnValueChanged(); if(_value == max && OnValueMax != null) OnValueMax(); if(_value == min && OnValueMin != null) OnValueMin(); } } } 

Thanks for any insights.

by Ryahes via /r/csharp

Leave a Reply