より大きなデータ/状態クラスの子であるデータ構造クラスがあります。
内部データ構造は、含まれているデータが変更されたときにイベントを発生させます。このイベントは、より大きなデータ/状態クラスによって消費されます。データ/状態クラスは、次のイベント ハンドラーに追加情報を渡すことができるように、独自のイベントを発生させます。
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;
最後に、2 番目のイベントを別のイベント ハンドラーで処理して、何らかの作業を行う必要があります。残念ながら、2 番目のイベントをトリガーする呼び出しは、多くの場合、NullReferenceException で失敗します。なんで?
----編集---- Null をチェックすると例外が防止されることを理解しています。そもそもこのイベントが Null である理由は混乱です = D