Pattatharasu Nataraj
Pattatharasu Nataraj

Reputation: 248

Text concatenation in vb.net

Hi i am trying to append text from a continuous receiving data The length of the text i am getting is like 1, 2, 5, 4, 1 etc.. i need first 5 byte of data i am trying to add it in another variable called K if length of K reaches the length of 5, I will move it to Variable J and will clear K

    Dim j = ""
    Dim l = ""
    Dim k As String
Private Sub ReceivedText(ByVal [text] As String) 'input from ReadExisting
    Dim payload As Byte()
    Dim station = TextBox1.Text
    Dim number As String = Regex.Replace([text], "[^0-9]", "")

    If number.Length > 0 Then
        K = K + number
        If K.Length = 5 Then
            j = K
            K = ""
        ElseIf k.Length > 5 Then
            j = K.Substring(0, 5)
            If j.Length = 5 Then
                l = K.Remove(5)
                K = ""
                K = l
            End If
        End If
        If Me.RichTextBox1.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.RichTextBox1.Text = K
        End If
    End If

if the variable K length is greater than 5, I will add 5 in the first variable to "J" and balance to another variable called l and append that to K, so i will get the first variable to stay constantly to the length of 5 but using the above code i cant get the desired result, any suggestions will be really appreciated

Upvotes: 1

Views: 457

Answers (2)

ajakblackgoat
ajakblackgoat

Reputation: 2149

You should declare K variable outside of the function so that the value will retain between function calls.

Dim K As String = ""

Sub Main
    ' This is to simulate stream of data to pass to ReceivedText function
    Dim arr As String() = {"0", "12", "34567", "8901", "2", "34", "56", "7890", "123", "456"}

    For Each data As String In arr
        ReceivedText(data)
    Next

    Console.WriteLine("End of data stream. Remaining chars (Incomplete) : " & K)

End Sub

Private Sub ReceivedText(ByVal [text] As String)
    Dim number As String = Regex.Replace([text], "[^0-9]", "")

    K &= number ' Append [text] to K

    ' If K length is 5 or more
    If K.Length >= 5 Then
        ' Get the first 5 characters and assign to J
        Dim J As String = K.Substring(0, 5)

        ' I just print the 5-char value (J) to console.
        Console.WriteLine("Completed text (5 chars) : " & J)

        ' Remove the first 5 characters from K
        K = K.Remove(0, 5)
    End If
End Sub

I simplify your code like above.

OUTPUT

Completed text (5 chars) : 01234
Completed text (5 chars) : 56789
Completed text (5 chars) : 01234
Completed text (5 chars) : 56789
Completed text (5 chars) : 01234
End of data stream. Remaining chars (Incomplete) : 56

Above, the Remaining chars is not number of characters remain in K, but the text 5 and 6. I purposely put extra characters in the stream of data as you can see in the arr last array item so they are not perfectly in blocks of 5.

Upvotes: 1

James Lee
James Lee

Reputation: 801

In the part of your code:

        ElseIf k.Length > 5 Then
            j = k.Substring(0, 5)
            If j.Length = 5 Then
                k = ""
                l = k.Remove(0, 5)
                k = l
            End If

Remove the k = "". Once removed, then your l variable will be set correctly (instead of blank). This should fix the issue. Good luck.

Upvotes: 1

Related Questions