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 3041
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 discussion thread is closed Replies have been disabled for this discussion. Similar topics
7 posts
views
Thread by Jonas |
last post: by
|
2 posts
views
Thread by Jakob Bengtsson |
last post: by
|
2 posts
views
Thread by Peter |
last post: by
|
11 posts
views
Thread by Brett |
last post: by
|
2 posts
views
Thread by Benign Vanilla |
last post: by
|
reply
views
Thread by Steve |
last post: by
|
4 posts
views
Thread by Shapper |
last post: by
|
6 posts
views
Thread by |
last post: by
|
6 posts
views
Thread by Yoavo |
last post: by
| | | | | | | | | | |