events : target-object is empty

C

Chris

Hi,

I add an event-handler to an event-object.
Then prior to raising the event do I want to print info regarding the
event-handlers 'attached' to the event-object :
the Method-information is print OK, but the Target-property is always empty
???

Public Delegate Sub MouseClickedHandler(ByVal obj As Object)
Class Mouse
Public Event MouseClickedEventHandler As MouseClickedHandler

Public Sub MouseClick()
Dim del As [Delegate]
For Each del In GetList()
Console.WriteLine("{0} {1}", del.Method, del.Target)
Next
RaiseEvent MouseClickedEventHandler(Me)
End Sub

Public Function GetList() As System.Delegate()
' Access the underlying delegate-object by 'pasting' the
' word 'event' to the object-name
Return MouseClickedEventHandlerEvent.GetInvocationList
End Function
End Class

Class ConsoleMessage
Public Shared Sub Show(ByVal obj As Object)
Console.WriteLine("ConsoleMessage.Show()")
End Sub
End Class

Class Program
Public Shared Sub Main()
Dim btn1 As New Mouse
Dim consMsg As New ConsoleMessage
AddHandler btn1.MouseClickedEventHandler, AddressOf consMsg.Show
btn1.MouseClick()
End Sub
End Class

Any ideas ?

Thnx

Chris
 
A

Armin Zingler

Chris said:
I add an event-handler to an event-object.
Then prior to raising the event do I want to print info regarding
the event-handlers 'attached' to the event-object :
the Method-information is print OK, but the Target-property is always
empty ???
[...]
Class ConsoleMessage
Public Shared Sub Show(ByVal obj As Object)
Console.WriteLine("ConsoleMessage.Show()")
End Sub
End Class

Class Program
Public Shared Sub Main()
Dim btn1 As New Mouse
Dim consMsg As New ConsoleMessage
AddHandler btn1.MouseClickedEventHandler, AddressOf
consMsg.Show btn1.MouseClick()
End Sub
End Class

Any ideas ?


Maybe because Sub Show is declared as Shared? Not a specific object is the
target. So, your code is equal to:

Public Shared Sub Main()
Dim btn1 As New Mouse
AddHandler btn1.MouseClickedEventHandler, AddressOf ConsoleMessage.Show
btn1.MouseClick()
End Sub
 
H

Herfried K. Wagner [MVP]

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