473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

XXXX_Load Event vs OnXXXX

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
5 2722
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
8
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that...
1
by: seash | last post by:
Is there any way to suppress an event method? i got Enter & leave Events on two groupBoxs, they r " private void groupBox1_Enter(object sender, System.EventArgs e)" and private void...
7
by: Charles Law | last post by:
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...
12
by: Jack Russell | last post by:
My unstanding of all VB up to and including vb6 is that an event could not "interrupt" itself. For instance if you had a timer event containing a msgbox then you would only get one message. ...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
9
by: jeff | last post by:
New VB user...developer... Situation...simplified... - I want to wrap a pre and post event around a system generated where the pre-event will always execute before the system event and the...
19
by: Daniela Roman | last post by:
Hello, I try to fire an event under a button click event and maybe anybody can give a clue please. I have let's say a WEB grid with PageIndexChanged event: private void...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.