473,396 Members | 1,917 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,396 software developers and data experts.

WithEvents question

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

Nov 20 '05 #1
8 3160
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
Nov 20 '05 #2
* "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>
Nov 20 '05 #3
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.
Nov 20 '05 #4
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.

Nov 20 '05 #5
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

Nov 20 '05 #6
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.
Nov 20 '05 #7
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.

Nov 20 '05 #8
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.
Nov 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
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...
2
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...
2
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...
11
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...
2
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...
0
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...
4
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...
6
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?
6
by: Yoavo | last post by:
Hi, Is there an equivalent code for "WithEvents" (of VB6) in CSharp ? Yoav.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...

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.