Question Vb2010: what is meant by withevent

softhard

Active member
Joined
Sep 29, 2012
Messages
35
Programming Experience
Beginner
Hello,

I am very beginner in VB.Net and i have been developing a GUI. I have a dll file which i would like to use in this project. Mean While, i came across a line in some code which i did not understand. it is like this

'Public withevents rep as New someclass'

Can any one help what does that mean? or any source would be helpful?

Thank you.
 
Hi,

If you are new to VB and this is not causing you an issue right now then I would suggest that you ignore what you may have read in some other code until you need to understand this.

Simply put, when you create your own classes you can make your own class raise events (same as a TextBox.TextChanged event) based on situations occurring within your own class. Until you start to make your own classes you should not worry about it.

Cheers,

Ian
 
The WithEvents keyword means that a variable can appear in the Handles clause of a method. For example, when you add a Button to a form, you usually want something to happen when the user clicks it, right? To do that you need to handle its Click event. Generally you will double-click the Button in the designer and that will generate a Click event handler like this:
VB.NET:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) [B][U]Handles Button1.Click[/U][/B]
The highlighted part means that that method handles the Click event of whatever object is currently assigned to the Button1 variable. In order that that's possible, when you added the Button to the form, the IDE generated a member variable like this:
VB.NET:
Private [B][U]WithEvents[/U][/B] Button1 As Button
Without that WithEvents keyword you could not use the Handles clause on the method and you'd have to use a slightly different way to handle the event.

For the moment, as IanRyder says, you probably don't really need to worry about this too much. Most of the objects whose events you'll be handling will be controls and components that you have added to the form in the designer. Sometimes though, you will need to declare a member variable yourself and, if you want to handle an event of the object you assign to that variable, you'll most likely want to declare it WithEvents.
 


You can get help from the example below:

Here is a typical use of withevents:


class C1
public WithEvents ev as new EventThrower()
public sub catcher() handles ev.event
Debug.print("Event")
end sub
end class
Here is a class which doesn't use WithEvents and is approximately equivalent. It demonstrates why WithEvents is quite useful:

class C2
private _ev as EventThrower
public property ev as EventThrower
get
return _ev
end get
set(byval value as EventThrower)
if _ev isnot nothing then
removehandler _ev.event, addressof catch
end if
_ev = value
if _ev isnot nothing then
addhandler _ev.event, addressof catch
end if
end set
end property
public sub new()
ev = new EventThrower()
end sub
public sub catcher()
Debug.print("Event")
end sub
end class
 
Back
Top