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

Remove Event Handlers C# style

I appreciate anyones help here. I've done a whole ton of cool things
in C# and am having difficulty expressing those things in VB.

In a program I wrote in C# (which I no longer have source for) I would
use the following line of code to dereference an event from all its
handlers:

myEvent = null;

How do I do this in VB.net 2.0? Using RemoveHandler I have to know
specifics about the EventHandler. I don't want to have to know this, I
want to dereference all the event handlers in the body of the class
that contains the event.

Class DelegateClass

Public Delegate Sub MyDelegate(ByVal sender As Object, ByVal e As
EventArgs)

Public Event MyEvent As MyDelegate
Public Sub OnMyEvent()
RaiseEvent MyEvent(Me, Nothing)
End Sub

Public Sub Close()
'***REMOVE ALL HANDLERS HERE***
End Sub

End Class

Class HandlerClass

Public Sub Func1(ByVal sender As Object, ByVal e As EventArgs)
Console.WriteLine("Fire Phasers!")
Console.Read()
End Sub

Public Sub New()

End Sub

End Class

Module Module2
Public Sub Main()
Dim dc As New DelegateClass
Dim hc As New HandlerClass()

Dim meh As DelegateClass.MyDelegate = AddressOf hc.Func1
AddHandler dc.MyEvent, meh

dc.OnMyEvent()

dc.Close()
End Sub

End Module
Thanks for the help!

Cheers
Dinsdale

Jul 13 '07 #1
2 2790
On 13 Lug, 18:14, Dinsdale <russ.ha...@gmail.comwrote:
I appreciate anyones help here. I've done a whole ton of cool things
in C# and am having difficulty expressing those things in VB.

In a program I wrote in C# (which I no longer have source for) I would
use the following line of code to dereference an event from all its
handlers:

myEvent = null;

How do I do this in VB.net 2.0? Using RemoveHandler I have to know
specifics about the EventHandler. I don't want to have to know this, I
want to dereference all the event handlers in the body of the class
that contains the event.

Class DelegateClass

Public Delegate Sub MyDelegate(ByVal sender As Object, ByVal e As
EventArgs)

Public Event MyEvent As MyDelegate

Public Sub OnMyEvent()
RaiseEvent MyEvent(Me, Nothing)
End Sub

Public Sub Close()
'***REMOVE ALL HANDLERS HERE***
End Sub

End Class

Class HandlerClass

Public Sub Func1(ByVal sender As Object, ByVal e As EventArgs)
Console.WriteLine("Fire Phasers!")
Console.Read()
End Sub

Public Sub New()

End Sub

End Class

Module Module2
Public Sub Main()
Dim dc As New DelegateClass
Dim hc As New HandlerClass()

Dim meh As DelegateClass.MyDelegate = AddressOf hc.Func1
AddHandler dc.MyEvent, meh

dc.OnMyEvent()

dc.Close()
End Sub

End Module

Thanks for the help!

Cheers
Dinsdale

Well I think you are doing a little mess there. :-) Anyway to stick
right to your
point and place the code within your Close sub, I would guess :

-------------------------------------------------------

Class DelegateClass

Public Delegate Sub MyDelegate(ByVal sender As Object, ByVal e As
EventArgs)
'Note: this is simply system.EventHandler
Public Event MyEvent As MyDelegate

Public Sub OnMyEvent()
RaiseEvent MyEvent(Me, Nothing)
End Sub

Public Sub Close(ByRef d As MyDelegate)
'***REMOVE ALL HANDLERS HERE***
RemoveHandler MyEvent, d
End Sub

End Class
Class HandlerClass

Public Sub Func1(ByVal sender As Object, ByVal e As EventArgs)
Console.WriteLine("Fire Phasers!")
Console.Read()
End Sub
Public Sub New()

End Sub

End Class

Module Module2

Public Sub Main()
Dim dc As New DelegateClass
Dim hc As New HandlerClass()

Dim meh As DelegateClass.MyDelegate = AddressOf hc.Func1
AddHandler dc.MyEvent, meh

dc.OnMyEvent()

dc.Close(meh)

End Sub

End Module

-----------------------------

Anyway I would never write the above piece of code. ;-)

-P


Jul 13 '07 #2
There is a simple VB equivalent to "myEvent = null".
It is "myEventEvent = Nothing". Note the extra "Event" - this refers to a
hidden VB variable associated with each event.
--
David Anton
www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Python
Instant C#
Instant VB
Instant C++
Instant Python
C++ to C# Converter
C++ to VB Converter
"Dinsdale" wrote:
I appreciate anyones help here. I've done a whole ton of cool things
in C# and am having difficulty expressing those things in VB.

In a program I wrote in C# (which I no longer have source for) I would
use the following line of code to dereference an event from all its
handlers:

myEvent = null;

How do I do this in VB.net 2.0? Using RemoveHandler I have to know
specifics about the EventHandler. I don't want to have to know this, I
want to dereference all the event handlers in the body of the class
that contains the event.

Class DelegateClass

Public Delegate Sub MyDelegate(ByVal sender As Object, ByVal e As
EventArgs)

Public Event MyEvent As MyDelegate
Public Sub OnMyEvent()
RaiseEvent MyEvent(Me, Nothing)
End Sub

Public Sub Close()
'***REMOVE ALL HANDLERS HERE***
End Sub

End Class

Class HandlerClass

Public Sub Func1(ByVal sender As Object, ByVal e As EventArgs)
Console.WriteLine("Fire Phasers!")
Console.Read()
End Sub

Public Sub New()

End Sub

End Class

Module Module2
Public Sub Main()
Dim dc As New DelegateClass
Dim hc As New HandlerClass()

Dim meh As DelegateClass.MyDelegate = AddressOf hc.Func1
AddHandler dc.MyEvent, meh

dc.OnMyEvent()

dc.Close()
End Sub

End Module
Thanks for the help!

Cheers
Dinsdale

Jul 13 '07 #3

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

Similar topics

9
by: VK | last post by:
My original idea of two trains, however pictural it was, appeared to be wrong. The truth seems to be even more chaotic. IE implements its standard down-up model: any mouse event goes from the...
2
by: Wavemaker | last post by:
The canonical way of declaring delegates for events is to include a parameter representing the sender as well as an EventArgs derived class (or EventArgs itself) as the second parameter...
3
by: Peter Oliphant | last post by:
In general, some controls can have events handlers attached to them, typically via the += operator. How does one remove an event handler added this way? Or, just as good for my purposes, how can...
9
by: Charles Law | last post by:
I have a form on which user controls are placed at runtime. When a control is added to the form a handler is added for an event that a high-level object raises, which must be handled by the new...
4
by: Charles Law | last post by:
Is there a way to dynamically remove an event handler from an event without knowing the name of the handler? For example, how can ClassB remove the handler without knowing the name, or how many...
16
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some...
14
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event...
7
by: Ron Goral | last post by:
Hello I am new to creating objects in javascript, so please no flames about my coding style. =) I am trying to create an object that will represent a "div" element as a menu. I have written...
5
by: jaysonnward | last post by:
Hello All: I've recently been recreating some 'dropdown menus' for a website I manage. I'm writing all my event handlers into my .js file. I've got the coding to work in Firefox, but the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.