CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2011
    Posts
    3

    Pass a method like a parameter of another method

    Hello !!!

    Sorry, I don't speak English very well. I hope you'll understand...

    Is there a way to choose between different methods and pass one into another method ?

    Sub conditionnal_binding(ByVal list_box_reference As ListBox, ByVal list_box_final As ListBox, _
    ByVal dico_source As Dictionary(Of String, List(Of String)), _
    ByVal my_methode As ??? )
    ' DO SOMETHING
    End Sub

    I wrote ??? because I don't know how to call another method inside a method.
    I would like to choose between different "sub-methods".
    Here is how I would like to use my method :

    conditionnal_binding(ListBox1, ListBox3, HTML.dico_basic_attribut, best_method)
    conditionnal_binding(ListBox1, ListBox3, HTML.dico_basic_attribut, some_method)
    conditionnal_binding(ListBox1, ListBox3, HTML.dico_basic_attribut, another_method)

    Help me please (If what I ask is possible)

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Pass a method like a parameter of another method

    We'd need some details, but one way is to create a separate CLASS and pass that between subs
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Feb 2011
    Posts
    3

    Re: Pass a method like a parameter of another method

    I call this method when a user select an item in a listbox :

    Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged
    ListBox2.SelectedIndex = ListBox1.SelectedIndex
    conditionnal_binding(...)
    End Sub

    I found a way to bind my list_box_final :
    I get the SelectedValue in another listbox
    I reset the list_box_final
    If dictionnary is not empty, call a method (who add value to dictionnary)
    For each related value in the dictionnary : add value to list_box_final

    My problem is how to call a different method each time I want to add value to 'another dictionnary'. Beause I have a lot of different dictionnaries and corresponding methods adding values.

    Here is all the code inside my method.

    Sub conditionnal_binding(ByVal list_box_reference As ListBox, ByVal list_box_final As ListBox, _
    ByVal dico_source As Dictionary(Of String, List(Of String)), _
    ByVal my_methode As ??? )

    Dim my_selected_tag As String = list_box_reference.SelectedValue
    list_box_final.Items.Clear()
    If dico_source.Count <= 0 Then
    HTML.add_dico_basic_attribut()
    End If
    For Each d In dico_source(my_selected_tag)
    list_box_final.Items.Add(d)
    Next

    End Sub

    My source code works. I just want to replace 'add_dico_basic_attribut()' by a parameter in 'conditionnal_binding'.

    I want to use"one" conditionnal_binding method with different parameters.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Pass a method like a parameter of another method

    Create a separate class, and you can substitute values
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Aug 2005
    Posts
    198

    Re: Pass a method like a parameter of another method

    Just use delegates:
    e.g.,
    Code:
    Public Delegate Sub MyDelegate(ByVal i As Integer)
    
    Public Sub Test(ByVal SubToUse As MyDelegate)
        'call the sub passed in:
        SubToUse(2)
    End Sub
    
    Public Sub CallingTestSub()
        'pass one sub:
        Test(AddressOf SpecificSubOne)
    
        'pass another sub:
        Test(AddressOf SpecificSubTwo)
    End Sub
    
    Public Sub SpecificSubOne(ByVal i As Integer)
        '....
    End Sub
    
    Public Sub SpecificSubTwo(ByVal i As Integer)
        '....
    End Sub
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  6. #6
    Join Date
    Feb 2011
    Posts
    3

    Re: Pass a method like a parameter of another method

    I'm really bad with delegates
    I tried to pass a delegate as function's parameter.

    Sub conditionnal_binding(ByVal list_box_reference As ListBox, ByVal list_box_final As ListBox, _
    ByVal dico_source As Dictionary(Of String, List(Of String)), _
    ByRef call_sub As my_delegate)

    I got an error : Expression does not produce a value.
    Normal, because it is a sub method
    And inside function, this parameter want a value

    conditionnal_binding(ListBox13, ListBox15, HTML.HTML_meta_info_basic_property, my_test(AddressOf HTML.add_structure_HTML_basic_property))

    Public Delegate Sub my_delegate()

    Public Sub my_test(ByVal my_method As my_delegate)
    my_method()
    End Sub

Tags for this Thread

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

Featured