Question Event as parameter?

fabio

Member
Joined
Jun 12, 2011
Messages
13
Programming Experience
5-10
Hi, how can I fix this code?

Public Class BWorker
Dim bw As New BackgroundWorker
Delegate Sub BW_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
Delegate Sub BW_Progress(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs)

Public Sub Init(ByVal func As BW_DoWork, ByVal progress As BW_Progress)
AddHandler bw.DoWork, AddressOf func ' <--- func is wrong
EndSub
EndClass
 
You don't need to define a delegate because the appropriate delegate is already defined. The BackgroundWorker.DoWork event is type System.ComponentModel.DoWorkEventHandler so that's what type your method parameter needs to be. Inside the method, the parameter IS the delegate, so you don't use AddressOf. AddressOf is used to create a delegate that refers to a method, so you would use AddressOf to create the delegate to pass to the parameter od the method.
Public Sub Init(ByVal doWorkHandler As DoWorkEventHandler, ByVal progressChangedHandler As ProgressChangedEventHandler)
    AddHandler bw.DoWork, doWorkHandler
    AddHandler bw.ProgressChanged, progressChangedHandler
End Sub
myBWorker.Init(AddressOf HandleDoWorkEvent, AddressOf HandleProgressChangedEvent)
 
I have now a better understanding of this, thank you very much for providing such a very clear explanation.

Fabio
 
Last edited:
Back
Top