Can I Dynamically (Programmatically?) Reference A Method?
I hope I can explain this in a coherent fashion.
I have the following code:
VB Code:
Private Function GetANewBGWorker() As BackgroundWorker
Dim bgworker As New BackgroundWorker() With {.WorkerSupportsCancellation = True}
AddHandler bgworker.DoWork, AddressOf WorkerDoWork
Return bgworker
End Function
Say I wanted to programatically assigned the "AddressOf". What Type would i put for the "ByVal BlahBlah as Blah" section? Is
Re: Can I Dynamically (Programmatically?) Reference A Method?
Code:
Private Sub bgworker_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs)
End Sub
Is that what you are asking for? If not then the coherency needs a little work, because I don't understand what you are asking.
Re: Can I Dynamically (Programmatically?) Reference A Method?
He would like to pass WorkerDoWork as a parameter to the function creating a background worker and is wondering what the parameter type would be.
Code:
Private Function GetANewBGWorker(WorkderDoWork As ????) As BackgroundWorker
Dim bgworker As New BackgroundWorker() With {.WorkerSupportsCancellation = True}
AddHandler bgworker.DoWork, AddressOf WorkerDoWork
Return bgworker
End Function
I think a delegate would normally be used to pass a sub or function to a sub or function, but I haven't tried it, and I don't know that a delegate would work in a AddressOf situation, but I haven't even tried to do a search on the subject yet.
Also lamdas seems like another possible candidate, but I guess I don't have the time or inclination to research either at the moment.
Re: Can I Dynamically (Programmatically?) Reference A Method?
Correct Passel. and also, delegates do NOT work to my knowledge there.
Re: Can I Dynamically (Programmatically?) Reference A Method?
Quote:
Originally Posted by
passel
He would like to pass WorkerDoWork as a parameter to the function creating a background worker and is wondering what the parameter type would be.
O.. well that makes much more sense.
Try this..
vb.net Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
GetANewBGWorker(AddressOf BackgroundWorker1_DoWork)
End Sub
Private Function GetANewBGWorker(WorkderDoWork As DoWorkEventHandler) As BackgroundWorker
Dim bgworker As New BackgroundWorker() With {.WorkerSupportsCancellation = True}
AddHandler bgworker.DoWork, WorkderDoWork
Return bgworker
End Function
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs)
'blah...
End Sub
I credit TnT for showing me that in this thread
Re: Can I Dynamically (Programmatically?) Reference A Method?
Quote:
Originally Posted by
ByteKnight
Correct Passel. and also, delegates do NOT work to my knowledge there.
Why the heck not? That's all event handlers are... delegates.
Seems to me delegate would be the way to go.
-tg
Re: Can I Dynamically (Programmatically?) Reference A Method?
Quote:
Originally Posted by
techgnome
Why the heck not? That's all event handlers are... delegates.
Seems to me delegate would be the way to go.
-tg
Would you be willing to show me an example pertaining to my question? Thank you!
Re: Can I Dynamically (Programmatically?) Reference A Method?
Don't need to - Kebo did... The second parameter to AddHandler requires a delegate... a delegate is really a pointer to a function... that's why you have to use "AddressOf" ... so that you pass the AddressOf (or the pointer) to that function. Event handler essentially work the same way. In fact, that's what th Handles clause does on the back end.
-tg
Re: Can I Dynamically (Programmatically?) Reference A Method?
Kebo's code didnt work (unless I'm doing something wrong). I wasn't able to input a method into the parameters.
Re: Can I Dynamically (Programmatically?) Reference A Method?
Quote:
Originally Posted by
ByteKnight
Kebo's code didnt work (unless I'm doing something wrong). I wasn't able to input a method into the parameters.
What does that mean... It doesn't work? My son does't work either, but it's probably because I am doing something wrong. I'm not trying to sound snarky, but if it's not working just saying it doesn't work doesn't give anyone anyway to help you.
Re: Can I Dynamically (Programmatically?) Reference A Method?
Quote:
Originally Posted by
ByteKnight
Kebo's code didnt work (unless I'm doing something wrong). I wasn't able to input a method into the parameters.
Quote:
Originally Posted by
kebo
What does that mean... It doesn't work? My son does't work either, but it's probably because I am doing something wrong. I'm not trying to sound snarky, but if it's not working just saying it doesn't work doesn't give anyone anyway to help you.
Ummm... yeah... I don't know these rumors get started but we're not mind readers and we're not sitting in your lap (not that I'm aware of... admittedly I can only speak for myself)... so we cannot see what you're seeing. saying something didn't work is like walking into the vet's office and saying "it hurts" ... he's got no idea what you're talking about since you didn't walk in with a pet and it's not going to do you any good because it's your arm that's broken. We've established the fact that it doesn't work. Ok, great... what does that mean? Did something happen that shouldn't have? Did something not happen that should have? Did the tires come off your car? Your doors suddenly fall off? Did ir rain orange juice? Did your fingers fall off? That's my favorite option because it's usually right, as it's the only reasaon I can come up with for why people don't provide more details. That must be it then. Your fingers fell off and you can't use them to type. That's why there's a lack of details. Well, all I can offer then is for you to see if you can take them somehow, scoop them up I guess, and take them to the doc's. Then you can walk in and say "it hurts" ... hopefully then he'll be able to figure it out.
-tg
Oh, and for the record... you can't just pass any method to it... you have to supply a method that conforms to the delegate signature... so if the signature requires parameters to be passed to it... so does your method. Apples to apples and oranges to oranges. You can't simply pass in grapefruit if what's expected is a lemon. Both maybe fruit, both may be (arguably) tropical fruit... but a grapefruit is not a lemon.
-tg