By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,510 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,510 IT Pros & Developers. It's quick & easy.

XXXX_Load Event vs OnXXXX

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.


"Charles Law" <bl***@nowhere.com> wrote in message
news:e5*************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #2

P: n/a
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
"Bill McCarthy <no spam>" <bill_mcc @ iprimus.com.au> wrote in message
news:OB**************@TK2MSFTNGP11.phx.gbl...
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.


"Charles Law" <bl***@nowhere.com> wrote in message
news:e5*************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #3

P: n/a
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.
Nov 20 '05 #4

P: n/a
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.

"Charles Law" <bl***@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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
"Bill McCarthy <no spam>" <bill_mcc @ iprimus.com.au> wrote in message
news:OB**************@TK2MSFTNGP11.phx.gbl...
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.


"Charles Law" <bl***@nowhere.com> wrote in message
news:e5*************@TK2MSFTNGP10.phx.gbl...
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



Nov 20 '05 #5

P: n/a
Thanks for the extra detail Bill.

Cheers.

Charles
"Bill McCarthy <no spam>" <bill_mcc @ iprimus.com.au> wrote in message
news:eT**************@TK2MSFTNGP10.phx.gbl...
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.

"Charles Law" <bl***@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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
"Bill McCarthy <no spam>" <bill_mcc @ iprimus.com.au> wrote in message news:OB**************@TK2MSFTNGP11.phx.gbl...
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.


"Charles Law" <bl***@nowhere.com> wrote in message
news:e5*************@TK2MSFTNGP10.phx.gbl...
> 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
>
>



Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.