Remove Event Handlers C# style

D

Dinsdale

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
 
P

pamela fluente

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
 
G

Guest

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
 
D

Dinsdale

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 Antonwww.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

Pamela: You are correct, this isn't a very friendly pattern, but it
works as a great example.

David: That hidden variable thing kinda freaks me out. Do you have any
more information on this? Is there any Microsoft documentation about
this in MSDN?

Sincerely,
Dinsdale
 
G

Guest

I know what you mean, but VB uses all sorts of 'freaky' devices like this
since it was mostly developed ad-hoc over the years.
Is it more freaky than the undeclared function return variable (i.e., using
'<method> = <value>' instead of 'Return <value>') ?
Is it more freaky than using 'Nothing' with value types?
--
David Anton
www.tangiblesoftwaresolutions.com
Convert between VB, C#, and C++
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
 
D

Dinsdale

I know what you mean, but VB uses all sorts of 'freaky' devices like this
since it was mostly developed ad-hoc over the years.
Is it more freaky than the undeclared function return variable (i.e., using
'<method> = <value>' instead of 'Return <value>') ?
Is it more freaky than using 'Nothing' with value types?
--
David Antonwww.tangiblesoftwaresolutions.com
Convert between VB, C#, and C++
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter

Good point. I'll put my blinders back on and forget I saw anything.

Thanks again!
Dinsdale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top