views:

1053

answers:

7

In my code behind I wire up my events like so:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    btnUpdateUser.Click += btnUpateUserClick;
}

I've done it this way because that's what I've seen in examples. Does the base.OnInit() method need to be called? Will it be implicitly be called? Is it better to call it at the beginning of the method or at the end? What would be an example where confusion over the base method can get you in trouble?

A: 

In this case, if you don't call the base OnInit, then the Init even will not fire.

In general, it is best practice to ALWAYS call the base method, unless you specifically know that you do not want the base behaviour to occur.

Whether its called at the start or the end depends on how you want things to work. In a case like this, where you are using an override instead of hooking up an event handler, calling it at the start of the method makes more sense. That way, your code will run after any handlers, which makes it more emulate a "normal" event handler.

Ch00k
A: 

Although the official framework design guidelines recommend otherwise, most class designers will actually make the OnXxx() method responsible for firing the actual event, like this:

protected virtual void OnClick(EventArgs e)
{
    if (Click != null) Click(this, e);
}

... so if you inherit from the class and don't call base.OnClick(e), the Click event will never fire.

So yes, even though this shouldn't be the case according to the official design guidelines, I think it's worth calling base.OnInit(e) just to be sure.

Matt Hamilton
A: 

official framework design guidelines recommend otherwise

They do? I'm curious, i've always thought the opposite, and reading Framework Design Guidelines and running FxCop has only cemented my view. I was under the impression that events should always be fired from virtual OnXxx() methods, that take an EventArgs parameter

Ch00k
+3  A: 

I should clarify:

The guidelines recommend that firing an event should involve calling a virtual "OnEventName" method, but they also say that if a derived class overrides that method and forgets to call the base method, the event should still fire.

See the "Important Note" about halfway down this page:

Derived classes that override the protected virtual method are not required to call the base class implementation. The base class must continue to work correctly even if its implementation is not called.

Matt Hamilton
A: 

Thanks Matt, that article has me more confused now because of that Important Note.

Is it better to go the Page_Init(object sender, EventArgs e) route instead?

Scott Muc
A: 

You probably are better off doing it that way, then this debate goes away. The article is interesting though, especially considering that the .NET Framework doesn't honour this guideline.

Ch00k
A: 

@Ch00k and @Scott I dunno - I like the OnEventName pattern myself. And yeah, I'm one of the people who are guilty of firing the event from that method.

I think overriding the On* method and calling the base one is the way to go. Handling your own events seems wrong somehow.

Matt Hamilton