Solved

Which is better WithEvents or AddHandler?

Posted on 2014-12-29
5
111 Views
Last Modified: 2014-12-29
I have a timer control in a windows app and was wondering what is more accepted...declaring a variable as

     Private WithEvents tmr As New Timer

Open in new window


or

AddHandler tmr.Tick, AddressOf tmr_Tick

Open in new window


Thanks!
0
Comment
Question by:BlakeMcKenna
5 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 250 total points
ID: 40521924
Unless you're going to add and remove (using removehandler) your reference, go with the withevents setup and the framework will manage it for you.
Also, if you were using shared events, you would need to use add/remove handler.
See http://msdn.microsoft.com/en-us/library/stf7ebaz(v=vs.90).aspx
and http://msdn.microsoft.com/en-us/library/6yyk8z93(v=vs.90).aspx for the MS doci on it.
0
 
LVL 32

Assisted Solution

by:it_saige
it_saige earned 250 total points
ID: 40521945
WithEvents allows for you to specify the handler directly on the method,  i.e. -
Imports System.Windows.Forms

Module Module1
	Private WithEvents tmr1 As New Timer()
	Private tmr2 As New Timer()

	Sub Main()
		AddHandler tmr2.Tick, AddressOf OnTick
	End Sub

	Private Sub OnTick(ByVal sender As Object, ByVal e As EventArgs) Handles tmr1.Tick

	End Sub
End Module

Open in new window

Shows in the designer as such:Capture.JPGNotice how tmr2 does not appear as a selectable timer object after we type Handles.  Adding WithEvents to tmr2, i.e. -
Imports System.Windows.Forms

Module Module1
	Private WithEvents tmr1 As New Timer()
	Private WithEvents tmr2 As New Timer()

	Sub Main()
	End Sub

	Private Sub OnTick(ByVal sender As Object, ByVal e As EventArgs) Handles 

	End Sub
End Module

Open in new window

Changes that behaviour:Capture.JPGWithout WithEvents, you have to specify AddHandler in order to capture the events raised by methods/controls.

-saige-
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40521950
Thanks for the explanations!
0
 
LVL 32

Expert Comment

by:it_saige
ID: 40521951
More on WithEvents and the Handles clause:

http://msdn.microsoft.com/en-us/library/stf7ebaz%28v=vs.90%29.aspx

-saige-
0
 
LVL 40
ID: 40522134
WithEvents is a little easier to use, but AddHandler is often better because it enables you to do a few things that WithEvents cannot do. The same goes for the Handles clause that Visual Studio automatically adds when you create an event procedure in the "usual" way. The Handles can be deleted and replaced by AddHandler, to be able to profit from the following.

You can decide at which point you activate the event. I have often seen programmers having performance problems when loading or refreshing a DataGridView. This is because the validation events are called on each cell, even if you do it with one line of code by setting the Datasource property. If you have tight validations in the grid, this can be a lot of code to run. Most of the time, this is useless, because you are feeding the grid with data that comes from a source where it has already been validated, such as a database. Validation is then necessary only when the user types. Working with AddHandler, you can delay the activation of the events. The grid fills a lot faster if you wait after it is full before calling AddHandler. Same thing when you fill a ComboBox or a ListBox.

You are not limited to use AddHandler with variables defined at the class scope as is the case with WithEvents. It can be useful sometimes to handle events on a local variable, something that you cannot do with WithEvents
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now