By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,561 Members | 3,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,561 IT Pros & Developers. It's quick & easy.

Remove Event Handlers C# style

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.