将多个事件连接到c#中的同一个方法

时间:2014-10-06 06:32:32

标签: c# events methods eventhandler

我有三个这样的事件:

 public static event EventHandler BelowLowLimit;
 public static event EventHandler AboveHighLimit;
 public static event EventHandler PercentBelowLowLimit;

     protected virtual void OnThresholdReached()
    {
        EventHandler handler = null;
        if (rs.Value < rs.LowLimit)
        {
            handler = BelowLowLimit;
        }
        else if (rs.Value > rs.UpperLimit)
        {
            handler = AboveHighLimit;
        }
        if (handler != null)
        {
            handler(this, new EventArgs());
        }
    }

我必须根据以下条件提出事件:

public long Rs
    {
        get { return rs.Value; }
        set
        {
            if (rs.Value < rs.LowLimit)
            {
                OnThresholdReached();
            }
            else if (rs.Value > rs.UpperLimit)
            {
                OnThresholdReached();
            }
            else
            {

                this.rs.Value = value;
            }

        }
    }

我想将此事件连接到同一方法以引发事件。这是正确的方法吗?

请建议我怎么做?

1 个答案:

答案 0 :(得分:0)

你在看这样的事吗?

        protected virtual bool CheckForThresholds(long rsValue, long rsLowLimit, long rsUpperLimit)
        {
            EventHandler handler = null;
            if (rsValue < rsLowLimit)
            {
                handler = BelowLowLimit;
            }
            else if (rsValue > rsUpperLimit)
            {
                handler = AboveHighLimit;
            }
            if (handler != null)
            {
                handler(this, new EventArgs());
                return true;
            }

            return false;
        }

        public long Rs
        {
            get { return rs.Value; }
            set
            {
                if (!CheckForThresholds(value, rs.LowLimit, rs.UpperLimit))
                {
                    this.rs.Value = value;
                }
            }
        }