Passing Event Delegate as an argument

K

Kerry Jenkins

I am having problems passing an Event Delegate as an argument to a
method that accepts a delegate argument. I get the following error
message:

'Public Event ProgressChanged(sender As Object, e As
System.EventArgs)' is an event, and cannot be called directly. Use a
'RaiseEvent' statement to raise an event.

I don't want to raise the event, but rather pass the delegate
associated with an event to another method.

Here is some sample code to demonstrate the problem:

Imports System.ComponentModel

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

Public Class BackgroundWorker
Inherits Component

Public Event ProgressChanged As ProgressChangedEventHandler

Protected Overridable Sub OnProgressChanged(ByVal progressArgs As
EventArgs)
'**** Error occurs on the following line ******
ProcessDelegate(ProgressChanged, Me, progressArgs)
End Sub

Sub ProcessDelegate(ByVal del As [Delegate], ByVal args() As
Object)
Dim temp As [Delegate] = del
If (temp Is Nothing) Then
Exit Sub
End If

Dim delegates() As [Delegate] = temp.GetInvocationList()
Dim handler As [Delegate]
For Each handler In delegates
InvokeDelegate(handler, args)
Next

End Sub

End Class

I am attempting to convert Juval Lowy's .net 1.1 BackgroundWorker
component from C# to VB. I cannot get past the error above.

Article discussing this component:
http://www.code-magazine.com/Article.aspx?quickid=0403071

Link to the C# code:
http://www.idesign.net/idesign/uploads/Background with 1.1.zip

Thank you,
Kerry Jenkins
 
J

Jay B. Harlow [MVP - Outlook]

Kerry,
'**** Error occurs on the following line ******
ProcessDelegate(ProgressChanged, Me, progressArgs)
As the message states, you are attempting to pass the Event itself to
ProcessDelegate. You need to pass the hidden Delegate field that VB.NET
creates for you.

Try something like:
'**** Error occurs on the following line ******
ProcessDelegate(ProgressChangedEvent, Me, progressArgs)

Hope this helps
Jay

Kerry Jenkins said:
I am having problems passing an Event Delegate as an argument to a
method that accepts a delegate argument. I get the following error
message:

'Public Event ProgressChanged(sender As Object, e As
System.EventArgs)' is an event, and cannot be called directly. Use a
'RaiseEvent' statement to raise an event.

I don't want to raise the event, but rather pass the delegate
associated with an event to another method.

Here is some sample code to demonstrate the problem:

Imports System.ComponentModel

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

Public Class BackgroundWorker
Inherits Component

Public Event ProgressChanged As ProgressChangedEventHandler

Protected Overridable Sub OnProgressChanged(ByVal progressArgs As
EventArgs)
'**** Error occurs on the following line ******
ProcessDelegate(ProgressChanged, Me, progressArgs)
End Sub

Sub ProcessDelegate(ByVal del As [Delegate], ByVal args() As
Object)
Dim temp As [Delegate] = del
If (temp Is Nothing) Then
Exit Sub
End If

Dim delegates() As [Delegate] = temp.GetInvocationList()
Dim handler As [Delegate]
For Each handler In delegates
InvokeDelegate(handler, args)
Next

End Sub

End Class

I am attempting to convert Juval Lowy's .net 1.1 BackgroundWorker
component from C# to VB. I cannot get past the error above.

Article discussing this component:
http://www.code-magazine.com/Article.aspx?quickid=0403071

Link to the C# code:
http://www.idesign.net/idesign/uploads/Background with 1.1.zip

Thank you,
Kerry Jenkins
 

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