XXXX_Load Event vs OnXXXX

C

Charles Law

I may have asked this before, but what is the purpose of both these
functions? Is there a time when one should be called over the other?

Does one supersede the other, or do they both have their time and place?

TIA

Charles
 
B

Bill McCarthy

Hi Charles,

I think you are referring to the XXXX Event versus the OnXXXX method, eg the
Load Event versus the OnLoad method.

To understand this pattern, it's probably easiest to look at a simple case.
Let's assume you create a Class named A and a class named B, where B derives
from A (that is:

Class A
...
End Class

Class B : Inherits A
..
End Class

Now to A, add an Event Foo. Once you do this, in B you can actually subscribe
to the event, but you cannot raise the event or block the event from raising.
This is because only code in class A can raise the event. The reason for that is
the event is actually a private multicast delegate and hence cannot be invoked
from outside of the class, due to scoping rules. So to allow your derived class
B to raise the event Foo as declared in A, the usual practice is to declare an
Overridable method in A called OnFoo. The code in that method actually raises
the event Foo. So from A, anywhere you would want to raise the event, you call
the OnFoo method. This also allows B to override the OnFoo method, and take
some action before and or after the event is raised, or even to stop the event
being raised. And this is the important thing here, that code in B's OnFoo
method that overrides it's base class A' OnFoo method, needs to pass the call to
it's base class for the event to be fired. You do this via the MyBase keyword,
eg MyBase.OnFoo. If you did not do this, then say another class C derived from
B well it would not have the Foo event raised and could not raise it, as the B's
OnFoo would be effectively blocking that event from being raised.

As to which to use, in a derived class, that being overriding the base class's
method, or using the event, well the event possibly has a slightly higher
overhead, but probably not a significant factor (IMO). The override of the
OnXXX method does have greater functionality, but it also means that you must be
careful to ensure you call the base method.

HTH's

Bill.
 
C

Charles Law

Hi Bill

It certainly does help. Thanks.

In the case of a Windows form, for example, that inherits from
System.Windows.Forms.Form, there is a Form1_Load event and an OnLoad
overridable function. If all I need to do is run some code when the form
loads, should I use one of these in preference to the other, or does it not
really matter in this case?

Charles
 
G

Guest

You should override the method of the base class instead of subscribing to an event.

When your code runs, an overriden methods can be called directly, whereas event subscribers must be enumerated and the specified delegate must be called. Overriding the proper method will save you several IL instructions.
 
B

Bill McCarthy

Hi Charles,

It can be six of one or half a dozen of the other in most cases. That is, if
you are not expecting your form to be derived from, then using the event is
simpler as it means you don't have to ensure the call to the base method gets
called.
Overriding the OnXXXX does require you to write more code, even if it is simply
calling the base class OnXXX. But it also gives you that extra flexibility in
knowing exactly when your code gets called either side of rasing the event. If
you think your form may be derived from, this can be crucial. The reason being
that although most probably, your form's XXX event subscribing code would get
called before any subscribing code in a further derived class, there is no
guarantee it will. So if you need to initialize object or fields before a
derived class accesses them, then the OnXXX override will give you that ability
with certainty, whereas events won't.

So as you can probably see, the OnXXX pattern is more powerful, and is probably
preferable, but that doesn't mean that the event subscription is necessarily a
bad thing, as long as you are aware of the limitations and requirements of both
;)

Bill.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top