Delegation with multiple parameters...

pasensyoso_manigbas

Well-known member
Joined
May 19, 2006
Messages
64
Programming Experience
Beginner
hi mods,

Good day!

i just want to ask something about delegation..

will it accept 2 or more parameters..?

see for example i want to make something like this:

Delegate Sub MyDelegate(byval x as listview, byval y as listviewitem)

I want to make use of this in my two listview object.
 
You don't pass the parameters to the delegate constructor. You just pass the address of a method that has the same signature. When you call Invoke on the delegate instance is when you pass the parameters, and Intellisense will tell you what you need.
 

Attachments

  • InvokeDelegateWithParameters.JPG
    InvokeDelegateWithParameters.JPG
    29.7 KB · Views: 43
Having said that, if you're using this to add an item to a ListView from a worker thread then there's no need to pass the ListView as a parameter unless you want to use the same method to add items to more than one ListView, which may be the case for you.
 
im doing it this way....

Delegate Sub Mydelegate(x as listview, y as listviewitem)
Private Sub DelegateView(ByVal x As ListView, ByVal y As ListViewItem)

If x.InvokeRequired Then
x.Invoke(New Mydelegate(AddressOf DelegateView), x)
Else
x.Items.Add(y)
End If
End Sub
geez... ive miss something... i only invoke the x...
Thanks buddy!!!!
 
Pay attention to Intellisense. See the screen shot below for what Intellisense would have been trying to tell you. If you're not going to do that then at least look up the documentation for the method you're using. From the help topic for Control.Invoke:
Executes the specified delegate, on the thread that owns the control's underlying window handle, with the specified list of arguments.
Don't just wait for inspiration to come from above. The second argument is a ParamArray, which means that you can either create an array and pass that:
VB.NET:
             x.Invoke(New Mydelegate(AddressOf DelegateView), New Object() {x, y})
or you can simply pass a loose list:
VB.NET:
             x.Invoke(New Mydelegate(AddressOf DelegateView), x, y)
That's how ParamArrays work. They are useful for methods where you want to be able to accept a number of arguments that will not be fixed until run time.
 

Attachments

  • InvokeDelegateWithParameters.JPG
    InvokeDelegateWithParameters.JPG
    29.3 KB · Views: 40
Back
Top