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.

LAamanni
  • 177
  • 2
  • 13

2 Answers2

5

I don't think you need <[ParamArray]()> in your code since it's already an array that you are passing:

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

And as far as passing the data through the invoke, don't use str(), just 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
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • That doesn't really solve anything. Exactly the same problem persists. I also followed this question: http://stackoverflow.com/questions/2640145/delegates-and-paramarray-workaround-suggestions "Delegates and ParamArray - Workaround Suggestions?" – LAamanni Sep 12 '13 at 06:23
  • Also I need to mention.. I am coming from another thread. I have a thread called "Engine" that is why I need this delegate in the first place. – LAamanni Sep 12 '13 at 06:55
1

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:

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 ParamArray from the Public Sub UpdateTextLabel -line.. But thanks for Kicking my head so I had the reason to go forward! :)

LAamanni
  • 177
  • 2
  • 13