Results 1 to 11 of 11

Thread: Can I Dynamically (Programmatically?) Reference A Method?

  1. #1

    Thread Starter
    Member
    Join Date
    May 2015
    Posts
    58

    Can I Dynamically (Programmatically?) Reference A Method?

    I hope I can explain this in a coherent fashion.

    I have the following code:

    VB Code:
    1. Private Function GetANewBGWorker() As BackgroundWorker
    2.         Dim bgworker As New BackgroundWorker() With {.WorkerSupportsCancellation = True}
    3.         AddHandler bgworker.DoWork, AddressOf WorkerDoWork
    4.         Return bgworker
    5.     End Function

    Say I wanted to programatically assigned the "AddressOf". What Type would i put for the "ByVal BlahBlah as Blah" section? Is

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    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

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    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.

  4. #4

    Thread Starter
    Member
    Join Date
    May 2015
    Posts
    58

    Re: Can I Dynamically (Programmatically?) Reference A Method?

    Correct Passel. and also, delegates do NOT work to my knowledge there.

  5. #5
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Can I Dynamically (Programmatically?) Reference A Method?

    Quote Originally Posted by passel View Post
    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:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.  
    3.         GetANewBGWorker(AddressOf BackgroundWorker1_DoWork)
    4.  
    5.     End Sub
    6.  
    7.     Private Function GetANewBGWorker(WorkderDoWork As DoWorkEventHandler) As BackgroundWorker
    8.         Dim bgworker As New BackgroundWorker() With {.WorkerSupportsCancellation = True}
    9.         AddHandler bgworker.DoWork, WorkderDoWork
    10.         Return bgworker
    11.     End Function
    12.  
    13.  
    14.     Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs)
    15.         'blah...
    16.  
    17.  
    18.     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

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Can I Dynamically (Programmatically?) Reference A Method?

    Quote Originally Posted by ByteKnight View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Member
    Join Date
    May 2015
    Posts
    58

    Re: Can I Dynamically (Programmatically?) Reference A Method?

    Quote Originally Posted by techgnome View Post
    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!

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Member
    Join Date
    May 2015
    Posts
    58

    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.

  10. #10
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Can I Dynamically (Programmatically?) Reference A Method?

    Quote Originally Posted by ByteKnight View Post
    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

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Can I Dynamically (Programmatically?) Reference A Method?

    Quote Originally Posted by ByteKnight View Post
    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 View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width