Object of type System.String cannot be converted to type System.String[]

secaro

Member
Joined
Sep 3, 2006
Messages
6
Programming Experience
Beginner
I have got this error message : Object of type 'System.String' cannot be converted to type 'System.String[]'

The part of the source code that gives me the error:

VB.NET:
    Private Sub DoRead2(ByVal ar As IAsyncResult)
        Dim allIn(1) As String
        Dim BytesRead As Integer
        Dim strMessage As String
        Dim strAddress As String
        Dim who As IPEndPoint = TCP2.Client.RemoteEndPoint
        strAddress = who.Address.ToString


        BytesRead = TCP2.GetStream.EndRead(ar)
        strMessage = Encoding.ASCII.GetString(ReadBuffer, 0, BytesRead)
        allIn(0) = strAddress
        allIn(1) = strMessage
        ProcessCommands(allIn)

        TCP2.GetStream.BeginRead(ReadBuffer, 0, ReadBufferSize, _
            AddressOf DoRead2, Nothing)
    End Sub

    Private Sub ProcessCommands(ByVal allIn() As String)
        If Me.TextBox1.InvokeRequired Then
            Dim d As New PrCommands(AddressOf ProcessCommands)

*******The line below gives me the error *****************
            Me.TextBox1.Invoke(d, allIn(0) + "-" + allIn(1))

        Else
            TextBox1.Text = allIn(0) + "--" + allIn(1)
        End If
    End Sub
But it comes up with this error message: Object of type 'System.String' cannot be converted to type 'System.String[]'

Im kinda new to .net, threads and sockets and i have gone thru the code several times without finding any solution.
If anyone have a clue about what i have done wrong or can explain the error message to me i would appreciate it.
 
ProcessCommands method takes a string array parameter, in your invoke you're trying to pass a single string. It should be:
VB.NET:
Me.TextBox1.Invoke(d, allIn)
 
Maybe there is a problem with your delegate definition. Who knows, you haven't posted that yet. Anyway, it must match the method signature you assign to it.
 
Delegate Sub PrCommands(ByVal allIn() As String)

I dont really know either. Im looking for a tutorial about threads, invoke where i can learn it. Most of the things i find on google is too advanced for me. I need the basics.
 
Well, that is a "Parameter Count Match". :confused:
 
that did not make it easier.

I think i'l just drop threading and invokes for this time. I have spent 5 days trying to figure it out.

I am used to use VB6. VS .net seems much heavier. Have to write so much code to do small things. Cryptic error messages.. I dont have that kind of patience.

Thanks for your help.:)
 
Sorry about that, it is in fact a well known bug, here are two quirks:
VB.NET:
TextBox1.Invoke(d, DirectCast(allIn, Object))
VB.NET:
TextBox1.Invoke(d, New Object() {allIn})
Go get'em! ;)
 
Back
Top