|
-
Jul 12th, 2015, 04:02 PM
#1
Thread Starter
Member
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
-
Jul 12th, 2015, 05:10 PM
#2
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.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Jul 12th, 2015, 05:51 PM
#3
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.
Last edited by passel; Jul 12th, 2015 at 05:57 PM.
-
Jul 12th, 2015, 05:54 PM
#4
Thread Starter
Member
Re: Can I Dynamically (Programmatically?) Reference A Method?
Correct Passel. and also, delegates do NOT work to my knowledge there.
-
Jul 12th, 2015, 07:35 PM
#5
Re: Can I Dynamically (Programmatically?) Reference A Method?
 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
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Jul 13th, 2015, 07:02 AM
#6
Re: Can I Dynamically (Programmatically?) Reference A Method?
 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
-
Jul 13th, 2015, 02:10 PM
#7
Thread Starter
Member
Re: Can I Dynamically (Programmatically?) Reference A Method?
 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!
-
Jul 13th, 2015, 02:33 PM
#8
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
-
Jul 14th, 2015, 10:21 AM
#9
Thread Starter
Member
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.
-
Jul 14th, 2015, 10:33 AM
#10
Re: Can I Dynamically (Programmatically?) Reference A Method?
 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.
Last edited by kebo; Jul 14th, 2015 at 10:39 AM.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Jul 14th, 2015, 10:57 AM
#11
Re: Can I Dynamically (Programmatically?) Reference A Method?
 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.
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|