473,757 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2869
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******** *****@TK2MSFTNG P10.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******** ******@TK2MSFTN GP11.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******** *****@TK2MSFTNG P10.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******** ********@tk2msf tngp13.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******** ******@TK2MSFTN GP11.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******** *****@TK2MSFTNG P10.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******** ******@TK2MSFTN GP10.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******** ********@tk2msf tngp13.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******** ******@TK2MSFTN GP11.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******** *****@TK2MSFTNG P10.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
7046
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
2888
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 that applies to all these events, but I need to have specific code execute when the form closes. The properties for this method are sender (the originator) and e (event arguments). I know how to get typeof (sender) to determine what form or...
8
6087
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 uses message queues to pass pointers to Events between threads. In my app there are simple events that can be defined using an enum (for example an event called NETWORK_TIMEOUT) and more complex events that contain data (for example an event called...
1
9298
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 groupBox1_Leave(object sender, System.EventArgs e) event methods, from msdn info the order of execution for them will be 1> Enter 2>GotFocus 3>lost focus 4>Leave 5>Validating i used only Enter and Leave events, after entering a groupBox i can see that...
7
390
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 time and place? TIA Charles
12
4140
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. However in vb.net you get continual messages (even setting the system modal property). Firstly, are these two assumptions right and if so what is the approved
41
4320
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 on some initialization information obtained elsewhere. Basically, I'm going to create my own dynamic toolbar where the toolbarbuttons can change. I'm not using the VB toolbar because of limitations in changing things like backcolor (I can't get...
9
2471
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 post event will always execuate after the system is completed... - I want to wrap this functionality in a framework, so I could possibly have 3 or 4 levels of inherited objects that need to have these pre / post events executed before and after the...
19
4757
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 DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
0
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.