我有三个这样的事件:
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;
}
}
}
我想将此事件连接到同一方法以引发事件。这是正确的方法吗?
请建议我怎么做?
答案 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;
}
}
}