WithEvents question

  • Thread starter Thread starter Bob Day
  • Start date Start date
B

Bob Day

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
 
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//PermaLink.aspx/1d7b3dd6-043b-4a9b-
8f18-a114c051e283
 
* "Bob Day said:
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'.
 
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?http://vbnet.mvps.org/dev/vb7/vbdotnet_ms_events.htm

Tom
 
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
 
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.
 
Back
Top