1

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.

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:

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:

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?

I would be very pleased if someone could explain and if I could understand how to do this.

4

2 回答 2

3

我认为您不需要<[ParamArray]()>在代码中,因为它已经是您要传递的数组:

Public Delegate Sub UpdateTextLabelDelegate(ByVal text() As String)

至于通过调用传递数据,不要使用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
于 2013-09-11T14:45:19.377 回答
1

终于设法解决了这个问题。这并不难,但我的错误是我自己的想法。

我没有对 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

所以,我所做的只是插入New Object() {args}到调用行并ParamArray从- 行中删除Public Sub UpdateTextLabel。但是感谢踢我的头,所以我有理由继续前进!:)

于 2013-09-12T08:58:57.017 回答