Having a senior moment. I’m trying do do an AddHandler for a method with a parameter. The method has the following signature
Private Sub GetListOfMails(sender as Thread, StartFolderitem as FolderItem)
How do I do the AddHandler?
AddHandler FileThread.Run, AddressOf GetListOfMails, FileThread, StartFolderitem
is giving me a type mismatch error.
Just do:
AddHandler FileThread.Run, AddressOf GetListOfMails
Your event definition needs to be:
GetListOfMails(StartFolderitem as FolderItem)
And your raise:
RaiseEvent FileThread.Run(StartFolderitem)
Martin_T
(Martin T)
February 17, 2018, 1:57pm
3
Or declare
Private Sub GetListOfMails(sender as Thread, StartFolderitem as Auto)
And call
AddHandler FileThread.Run, AddressOf GetListOfMails, FileThread, StartFolderitem
The problem is that is that addhandler doesn’t take the params, just the address:
AddHandler eventName, delegateMethod
Martin_T
(Martin T)
February 17, 2018, 2:00pm
5
[quote=373972:@]The problem is that is that addhandler doesn’t take the params, just the address:
AddHandler eventName, delegateMethod[/quote]
Sorry I was wrong, you were right Julian. My way describes the Xojo.Core.Timer.CallLater
Method!
Thanks, Julian, but this doesn’t work:
AddHandler FileThread.Run, AddressOf GetListOfMails
FileThread.Run(theFileSelection(currentSelection))
The first line makes a an error “expected Delegate(Thread), but got Delegate(Thread, Folderitem)”. The second line makes the error “too many arguments”.
DerkJ
(DerkJ)
February 17, 2018, 2:48pm
7
[quote=373971:@Martin T]Or declare
Private Sub GetListOfMails(sender as Thread, StartFolderitem as Auto)
And call
AddHandler FileThread.Run, AddressOf GetListOfMails, FileThread, StartFolderitem
i susped it should be this. As AddHandler takes only the address, it will pass on the called parameters.
@Derk Jochems: this was what I started with which doesn’t work.
You can’t pass a FolderItem into a handler for Thread.Run
@ : yes, I noticed that. There is no raise event in my code, however.
This thread has several misconceptions here.
First, in order to use add hander, you should match event definition.
Second, Thread.Run
take no argument. So, this is invalid definition.
Private Sub GetListOfMails(sender as Thread, StartFolderitem as FolderItem)
AddHandler eventName, delegateMethod
Fourth, delegateMethod
should in format
Sub delegateMethod(sender as <SenderObject>, <the_rest_of_event_definition>)
or
Function delegateMethod(sender as <SenderObject>, <the_rest_of_event_definition>) as datatype //again should match event definition
1 Like
@Asis Patisahusiwa : you are fully correct. But how do I get my folderitem into the thread?
The easy way is to extends thread, add property StartFolderitem as FolderItem
to the class.
Your code will be like this
dim th as New CustomThread
th.StartFolderitem = StartFolderitem
th.Run
Or create a subclass with an event getListOfMails(StartFolderItem as folderItem)
and in you run event you can raise theEvent.
where you create the addHandler then you can write:
AddHandler FileThread.getListOfMails, AddressOf GetListOfMails
and your GetListOfMails method can have the signature: Private Sub GetListOfMails(sender as Thread, StartFolderitem as FolderItem)
Another idea: subclass the Thread, implement a Protected Run method that just calls super.Run, then add a method that takes whatever parameters you need. (You can even call that method “Run” if you’d like.)
dim th as new CustomThread
th.Process( f ) // Process will store the parameter and call Run
// In the Thread subclass
Protected Sub Run ()
super.Run
End Sub
Sub Process (f As FolderItem)
MyFolderProp = f
Run
End Sub
The consumer will not be able to call Run directly on this subclass.
DerkJ
(DerkJ)
February 17, 2018, 3:43pm
16
Now it makes sense, nope you can’t change the method signature.
Thanks for the ideas, guys. I wanted to do without subclassing the thread but it seems that this is necessary. Will try tomorrow.