Whilst coding today I found myself realising that exposing public event handlers ( which are essentially public fields) is considered by many to be bad programming practice. Properties expose private fields so surely there’s a similar technique for exposing private event handlers?
public event EventHandler MyEvent
{
add { this.myEvent += value; }
remove { this.myEvent –= value; }
}
// Register Event Handler (and de-register) in the same manner.
SomeClass.MyEvent += this.OnMyEvent;
Event ‘accessors’ can also be used to allow external classes to register with user controls events (and obviously other private or internal events associated with .net Class Library Components). Quite why this golden nugget of information isn’t more easily accessible I have no idea. But then I might have been looking in the wrong places…
oddSprite

0 comments:
Post a Comment