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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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?
如果有人能解释并且我能理解如何做到这一点,我将非常高兴。
我认为您不需要<[ParamArray]()>
在代码中使用它,因为它已经是您传递的数组:
Public Delegate Sub UpdateTextLabelDelegate(ByVal text() As String)
Run Code Online (Sandbox Code Playgroud)
至于通过调用传递数据,不要使用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
Run Code Online (Sandbox Code Playgroud)