Design Guidelines for Class Library Developers wrote:
Use a protected (Protected in Visual Basic) virtual method to raise each event. This technique is not appropriate for sealed classes, because classes cannot be derived from them. The purpose of the method is to provide a way for a derived class to handle the event using an override. This is more natural than using delegates in situations where the developer is creating a derived class. The name of the method takes the form OnEventName, where EventName is the name of the event being raised.
Public Event Click(ByVal sender As Object, ByVal e As EventArgs) Public Overridable Sub OnClick(ByVal e As EventArgs) RaiseEvent Click(Me, e) End Sub |
Joe Smugeresky wrote:
This is what I was referring to. I don't like that design choice. Throwing extra methods in an abstract class in order to raise events. It doesnt make sense in an abstract base class. That was the point of the original post, sorry if you misunderstood some guy.
The reason you have to do this is that it is a security issue in .NET for code outside of the declaring type to fire events on that type. Because of this, the option you outlined is the only one available. All things considered, I'd rather be secure knowing that the events on a component are not being manipulated outside of that component.
Hope this helps.
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com