dcsimg
Results 1 to 5 of 5

Thread: Using a delegate

  1. #1

    Thread Starter
    New Member aasclav's Avatar
    Join Date
    Oct 2010
    Location
    Greece
    Posts
    12

    Using a delegate

    Hi all,

    Suppose we got a form with some checkboxes.
    Each ckeckbox represents a method of the same class.
    Each method takes no arguments, returns no values.
    Is it possible using a delegate to loop through the checkboxes and run only the methods being checked?

    Thanks

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Using a delegate

    It might be easier to use reflection and interfaces.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,796

    Re: Using a delegate

    possible? sure, I suppose... I mean it's possible I could win the lottery this weekend...
    The question that you should be asking is, what purpose would that serve? I'm not sure I get how a delegate would work in this case... reflection, that I get... maybe... but it depends on what you're trying to accomplish.

    -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??? *

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Using a delegate

    You could associate an Action with each CheckBox, and Invoke the Actions associated with the checked checkboxes. How you do this association is up to you, you could have a Dictionary(Of CheckBox, Action), you could use the Tag property, etc.

    Example:
    Code:
    Private checkboxes As Dictionary(Of CheckBox, Action)
    
    Private Sub SetupCheckboxes()
        checkboxes = New Dictionary(Of CheckBox, Action)()
        checkboxes.Add(CheckBox1, New Action(AddressOf Method1))
        checkboxes.Add(CheckBox2, New Action(AddressOf Method2))
        checkboxes.Add(CheckBox3, New Action(AddressOf Method3))
        checkboxes.Add(CheckBox4, New Action(AddressOf Method4))
    End Sub
    
    Private Sub ExecuteMethods() 'Call on button click or whatever
        For Each kvp As KeyValuePair(Of CheckBox, Action) In checkboxes
            Dim checkbox = kvp.Key
            Dim action = kvp.Value
    
            If checkbox.Checked Then action.Invoke()
        Next
    End Sub
    (Not very sure about syntax, it's been a while since I fired up VB)

    That's how you could do it, whether it's useful? I dunno... There are probably better ways to do what you want.

  5. #5

    Thread Starter
    New Member aasclav's Avatar
    Join Date
    Oct 2010
    Location
    Greece
    Posts
    12

    Re: Using a delegate

    Thanks Nick, first i will try reflection.
    Thank you

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width