我有一個數據結構類是一個更大的數據/狀態類的孩子。.net事件混亂
內部數據結構在包含的數據更改時觸發事件。這個事件被更大的數據/狀態類使用。數據/狀態類然後觸發它自己的事件,以便它可以將附加信息傳遞給下一個事件處理程序。
Public class Data
{
//properties and objects go here
public int Count
{
get { return _count; }
internal set
{
//if the count grew simply set _count
if (value != _oldCount)
{
_oldCount = _count;
_count = value;
}
//if the count shrank then set the count and trigger an event if the count is under 100
else
{
_oldCount = _count;
_count = value;
if (_count < 100)
{
CountChanged(this, new EventArgs());
}
}
}
}
public event EventHandler CountChanged;
}
上述事件此事件處理
Data.CountChanged += new EventHandler(DataCountChanged);
private void DataCountChanged(object sender, EventArgs e)
{
DataRemoved(this, e); //Handle the old event and trigger a new event to pass along more information
}
public event EventHandler DataRemoved;
最後第二個事件應該被另一事件處理程序進行處理做了一些工作消耗。不幸的是,觸發第二個事件的調用失敗了,而NullReferenceException往往不是。爲什麼?
----編輯---- 據我所知,檢查Null將防止異常。混亂就是爲什麼這個事件是擺在首位空= d
簡單,沒有人訂閱它的事件處理程序。不在你的代碼片段中。 – 2012-04-27 01:46:31
Data.CountChanged + = new EventHandler(DataCountChanged); 這不是事件的事件處理程序訂閱嗎? – Ritz 2012-04-27 02:23:09
不,它在DataRemoved上被轟炸。 – 2012-04-27 03:19:57