带有调用的 VB.net 委托参数数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18743358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.net delegate param array with invoke
提问by LAamanni
I have this code in my Parser and I want to pass text to Form1 so I can update some Labels or whatever. (My structure is as follows: Form1 -> Engine -> Parser) Sometimes I need to pass 2 strings, sometimes more.
我的解析器中有此代码,我想将文本传递给 Form1,以便我可以更新一些标签或其他内容。(我的结构如下:Form1 -> Engine -> Parser)有时我需要传递2个字符串,有时更多。
Public Class Parser
Public Event NewInfo(<[ParamArray]()> Byval strArray() as String)
Public Sub SomeParser(ByVal m As Match)
Dim strArray() As String = {"Word1", "Word2"}
RaiseEvent NewInfo(strArray)
End Sub
End Class
Then I have this another class. I pass the Array to Engine and after that, to Form1, finally:
然后我又上了一节课。我将数组传递给引擎,然后传递给 Form1,最后:
Public Class Engine
Private parent as Form1
Private WithEvents Parser As New Parser
Private Sub New(ByRef parent as Form1)
Me.parent = parent
EndSub
Private Sub ChangeLabel(ByVal str() As String) Handles Parser.NewInfo
parent.UpdateTextLabel(str)
End Sub
And then I have this in Form1:
然后我在 Form1 中有这个:
Public Class Form1
Private WithEvents Engine as New Engine(Me)
Public Delegate Sub UpdateTextLabelDelegate(<[ParamArray]()> ByVal text() As String)
Public Sub UpdateTextLabel(ByVal ParamArray str() As String)
If Me.InvokeRequired Then
Me.Invoke(New UpdateTextLabelDelegate(AddressOf UpdateTextLabel), str())
Else
(Do stuff here)
End Sub
End Class
The code stops at Me.invoke(New UpdateTextLabelDelegate).... -line. Exception is something like: System.Reflection.TargetParameterCountException So it means something like wrong amount of parameters.. How to do this properly?
代码在 Me.invoke(New UpdateTextLabelDelegate).... 行停止。异常是这样的: System.Reflection.TargetParameterCountException 所以它意味着参数数量错误..如何正确地做到这一点?
I would be very pleased if someone could explain and if I could understand how to do this.
如果有人能解释一下,如果我能理解如何做到这一点,我会很高兴。
回答by LarsTech
I don't think you need <[ParamArray]()>in your code since it's already an array that you are passing:
我认为您不需要<[ParamArray]()>在代码中,因为它已经是您正在传递的数组:
Public Delegate Sub UpdateTextLabelDelegate(ByVal text() As String)
And as far as passing the data through the invoke, don't use str(), just str
至于通过调用传递数据,不要使用str(),只是str
Public Sub UpdateTextLabel(ByVal str() As String)
If Me.InvokeRequired Then
Me.Invoke(New UpdateTextLabelDelegate(AddressOf UpdateTextLabel), str)
Else
'(Do stuff here)
End If
End Sub
回答by LAamanni
Finally managed to solve this problem. It wasn't that difficult but my mistake was something inside my own head.
终于设法解决了这个问题。这并不难,但我的错误是我自己的想法。
I made no changes to Parser.vb so the above code is Ok. Also, no changes to Engine.vb. Changes to Form1.vb are here:
我没有对 Parser.vb 做任何更改,所以上面的代码是好的。此外,对 Engine.vb 没有更改。对 Form1.vb 的更改如下:
Public Class Form1
Private WithEvents Engine as New Engine(Me)
Public Delegate Sub UpdateTextLabelDelegate(<[ParamArray]()> ByVal text() As String)
Public Sub UpdateTextLabel(ByVal str() As String)
If Me.InvokeRequired Then
Me.Invoke(New UpdateTextLabelDelegate(AddressOf UpdateTextLabel), New Object() {str})
Else
'(Do stuff here)
End Sub
End Class
So, all I did was to insert New Object() {args}to the invoke line and removed ParamArrayfrom the Public Sub UpdateTextLabel-line..
But thanks for Kicking my head so I had the reason to go forward! :)
所以,我所做的只是插入New Object() {args}到调用行并ParamArray从 -行中删除Public Sub UpdateTextLabel..但是感谢你踢我的头所以我有理由继续前进!:)

