Using VS 2003, VB.NET, MSDE
If an event is raised in a class that was NOT instantiated with the
WithEvents key word, is that event effectively ignored?
Or, another way to phrase the question, for a class that raises events, can
you sometimes instantiate using WithEvents when you want to catch the raised
event, and sometimes instantiate it without the key work WithEvents, when
you don't want to catch the rasied event?
I think think the answer to both questions is Yes.
Please advise.
Thanks!
Bob 8 3135
In article <#f**************@TK2MSFTNGP09.phx.gbl>, Bo****@TouchTalk.net
says... Using VS 2003, VB.NET, MSDE
If an event is raised in a class that was NOT instantiated with the WithEvents key word, is that event effectively ignored?
Or, another way to phrase the question, for a class that raises events, can you sometimes instantiate using WithEvents when you want to catch the raised event, and sometimes instantiate it without the key work WithEvents, when you don't want to catch the rasied event?
WithEvents is really a hold-over from VB6. However, if you don't define
a variable as "WithEvents", then you can't get the automatic "hook-up"
of events by using the "Handles" clause. But you can still catch events
by using AddHandler.
This link goes into some more detail: http://www.panopticoncentral.net//Pe...dd6-043b-4a9b-
8f18-a114c051e283
--
Patrick Steele
Microsoft .NET MVP http://weblogs.asp.net/psteele
* "Bob Day" <Bo****@TouchTalk.net> scripsit: If an event is raised in a class that was NOT instantiated with the WithEvents key word, is that event effectively ignored?
Or, another way to phrase the question, for a class that raises events, can you sometimes instantiate using WithEvents when you want to catch the raised event, and sometimes instantiate it without the key work WithEvents, when you don't want to catch the rasied event?
There is no event raised if no handlers are added to the event. You can
use 'AddHandler' to add a handler to an event, even if the variable
holding the reference to the object raising the event is not declared
'WithEvents'.
There are 2 different event models in VB.NET:
1. 'WithEvents' declaration + 'Handles' part in the event handler.
2. 'AddHandler', 'RemoveHandler', doesn't require 'WithEvents'.
--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Hi,
No, WithEvents is not required. You can use AddHandler.
The advantage to WithEvents is that Intelisense works for the events, and
you have one (or three) lines less code to write -- the prototype for the
event handler is built for you. WithEvents is a time-saver, IMO.
Dick
--
Richard Grier (Microsoft Visual Basic MVP)
See www.hardandsoftware.net for contact information.
Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
To the original poster... it is useful to recognize that events are not just
fired into the air (so the speak) they cause event handlers to be called by
the class raising the event. That's why (as Herfried points out) it isn't
that event is ignored, rather there is nobody to "raise" it to.
As to which syntax is preferred, personally I would recommend avoiding
WithEvents. Simply use AddHandler (and where needed RemoveHandler) with the
appropriate arguments. Every "freebie" you get through something that's
"automatic" involves overhead and the WithEvents declaration is no
exception.
For a good explanation on what is happening under the covers check out the
following: http://vbnet.mvps.org/index.html?htt..._ms_events.htm
Tom
"Dick Grier" <di**************@msn.com> wrote... Hi,
No, WithEvents is not required. You can use AddHandler.
The advantage to WithEvents is that Intelisense works for the events, and you have one (or three) lines less code to write -- the prototype for the event handler is built for you. WithEvents is a time-saver, IMO.
Dick
-- Richard Grier (Microsoft Visual Basic MVP)
See www.hardandsoftware.net for contact information.
Author of Visual Basic Programmer's Guide to Serial Communications, 3rd Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
Thanks everyone, that was helpful.
Bob Day
"Bob Day" <Bo****@TouchTalk.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... Using VS 2003, VB.NET, MSDE
If an event is raised in a class that was NOT instantiated with the WithEvents key word, is that event effectively ignored?
Or, another way to phrase the question, for a class that raises events,
can you sometimes instantiate using WithEvents when you want to catch the
raised event, and sometimes instantiate it without the key work WithEvents, when you don't want to catch the rasied event?
I think think the answer to both questions is Yes.
Please advise.
Thanks! Bob
Hi,
Have you looked at the IL to see that there is any difference between
WithEvents and AddHandler (once you also have coded that handler(s))? The
extra overhead, if there, has to be very small.
I remember, when .NET first came out, some discussion about this, and the
conclusion (if I recall correctly) was that there simply wasn't any
important difference. If so, my preference remains to write the smallest
amount of code. This make maintenance/understanding easier. All, IMO, of
course.
Dick
--
Richard Grier (Microsoft Visual Basic MVP)
See www.hardandsoftware.net for contact information.
Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
I'm not a "look at the IL" type of person... :-)
The differences include however that you cannot dynamically assign event
handlers using the WithEvents keyword. You cannot shut them off or redirect
them.
A quick check on the topic indicates that other things can get "confusing"
as someone posted that object.gettype.getfields.length will return a
different answer if you use the WithEvents modifier. It doesn't see it as a
field any longer but as a property.
I think people should do whatever they are happiest doing but I'll still
suggest that they consider the implications beforehand. As far I know there
is no WithEvents modifier in C# but there is AddHandler.
Tom
"Dick Grier" <di**************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... Hi,
Have you looked at the IL to see that there is any difference between WithEvents and AddHandler (once you also have coded that handler(s))? The extra overhead, if there, has to be very small.
I remember, when .NET first came out, some discussion about this, and the conclusion (if I recall correctly) was that there simply wasn't any important difference. If so, my preference remains to write the smallest amount of code. This make maintenance/understanding easier. All, IMO, of course.
Dick
-- Richard Grier (Microsoft Visual Basic MVP)
See www.hardandsoftware.net for contact information.
Author of Visual Basic Programmer's Guide to Serial Communications, 3rd Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
Hi,
As far I know there
is no WithEvents modifier in C# but there is AddHandler.
<<
True. And, this fact OFTEN is a pain. The implication is that C# does not
build event handler prototypes for any component. This slows programmer
productivity, IMO. However... I expect that this may change.
You cannot shut them off or redirect them.
<<
Those, actually, is the only reasons (IMO) to avoid WithEvents. And, you as
the programmer, know about this need in advance -- or if you don't know, a
minor edit will take care of it.
Dick
--
Richard Grier (Microsoft Visual Basic MVP)
See www.hardandsoftware.net for contact information.
Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jonas |
last post by:
This works fine in Win XP but does not work at all in Win 98.
Private WithEvents objIExplorer As InternetExplorer
I have to do it like this to get it to work in Win 98
Dim objIExplorer As...
|
by: Jakob Bengtsson |
last post by:
Hi,
I have a form (which cannot be serialized).
In the form's code I declare an object like this (never
mind the object nor class name, it's for illustration
only):
Private WithEvents...
|
by: Peter |
last post by:
Hello,
Thanks for reviewing my question. I am following KB article about creating
a custom control and its source code is in VB. I got to a part where I have
the following:
Private...
|
by: Brett |
last post by:
What is the C# equivalent for this VB.NET code:
Private WithEvents IE_Inst As New SHDocVw.InternetExplorer
I get as far as:
private SHDocVw.InternetExplorer IE_Inst = new...
|
by: Benign Vanilla |
last post by:
I am a ASP.NET newbie so please bear with me.
I had a series of webforms working on my web site, and then I made some
tragic error that is causing all kinds of problems. The main symptom is that...
|
by: Steve |
last post by:
Hello,
I have been experimenting/learning to use aspx with and
without VS2003. In VS 2003 I created a simple
webform/application which contains a listbox and a label.
I populate the listbox...
|
by: Shapper |
last post by:
Hello,
I have an aspx.vb page with this code:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents Menu1 As MenuControl.Web.UI.Menu
...
If I have my code in the aspx...
|
by: |
last post by:
Is there any additional overhead for declaring a variable using the
WithEvents keyword even if none of the events are being handled?
|
by: Yoavo |
last post by:
Hi,
Is there an equivalent code for "WithEvents" (of VB6) in CSharp ?
Yoav.
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |