委托中的多个杂项 [英] Multiple variales in delegate
问题描述
我一直在尝试找出如何向我的"委托中添加另一个变量
作为我的回调函数-但是经过数小时的网上搜索后,我仍然无法理解它是如何完成的:-(
所以这是我的问题:
如何扩展下面的代码以添加另一个变量(我可以在没有任何问题的委托语句[public proxy sub SetText ...]中做到这一点,但是当我创建它时,我无法将其传递给委托[Dim d as new SetText ...] ???)
感谢您的任何投入:)
I''ve been trying to figure out how to add another variable to "my" delegate
for my callback function - but after hours of searching the net I have not been able to understand how it''s done :-(
So here is my question:
How to I expand the code below to add another variable (I can do that In the delegate statement [public delegate sub SetText...]without problems but then i can not pass it to the delegate when I create it[Dim d as new SetText...] ???)
Thanks for any inputs :)
Delegate Sub SetTextCallback(ByVal [text] As String)
Public Sub SetPRGMessageText(ByVal [text] As String)
' InvokeRequired required compares the thread ID
' If the creator thread of the control is different then the caller Thread
' this Sub will be called by cretor thread of the control
If MenuStrip_Main.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetPRGMessageText) ' this is the call back to [SetPRGMessageText]using the creator
Me.Invoke(d, New Object() {[text]})
Else
Me.ToolStripTxB_Message.Text = [text]
End If
End Sub
推荐答案
在将附加参数添加到签名后,应该将附加参数传递给调用.
所以,这个:
You should just be passing the additional parameter into the invoke after you''re done adding it to your signature.
So, this:
Me.Invoke(d, New Object() {[text]})
成为:
Becomes:
Me.Invoke(d, New Object() {[text], [text2]})
这样的事情.
--------
更新:
这是一个有效的代码段,无论哪种语法,您都可以调用invoke ...
Something like that.
--------
Update:
Here''s a snippet that would work, with either syntax you can call invoke...
Public Class Form1
Delegate Sub SetTextCallback(ByVal A As String, ByVal B As String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim d As New SetTextCallback(AddressOf Method)
Invoke(d, "A", "B")
Invoke(d, New Object() {"One", "Two"})
End Sub
Private Sub Method(ByVal A As String, ByVal B As String)
Trace.Write(String.Format("{0} {1}", A, B))
End Sub
End Class
这篇关于委托中的多个杂项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!