-
February 4th, 2011, 07:41 PM
#1
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)
-
February 4th, 2011, 08:28 PM
#2
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
-
February 5th, 2011, 08:06 AM
#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.
-
February 5th, 2011, 12:21 PM
#4
Re: Pass a method like a parameter of another method
Create a separate class, and you can substitute values
-
February 5th, 2011, 09:47 PM
#5
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
-
February 9th, 2011, 04:59 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|