1

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

8
  • whats error you get or whats the problem..facing Commented Feb 14, 2019 at 7:41
  • no error, the variable k is not reaching the expected length of 5 even after concat Commented Feb 14, 2019 at 8:22
  • You dim k as string but then k<5 without assigning anything to k ? Commented Feb 14, 2019 at 8:48
  • I am a beginner to vb, if it is in a loop it will assign value on second execution right? Commented Feb 14, 2019 at 8:52
  • 1
    Dim k As String will initialize new variable with value equals Nothing. Commented Feb 14, 2019 at 8:58

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

4 Comments

Actually, the code as it is, only removing the k = "" line, should have worked. Your k.Remove usage was correct. I ran the code in debug, and the results appear as your requirements outline (e.g. if "text" is received as, say, "123456", then you will end up with j="12345", l="6", and finally k="6". Can you try to debug and have a break at k = l, and tell us what value you have for l, if you try using "123456" as your passed in string parameter value?
i dont know to debug, but if i place declaration before the class it works fine for the first time and loop getting stopped
Can you edit your question to include the change that you made (placing declaration before class) so we can see it? Also, by 'loop getting stopped', wanted to clarify that you mean that you are still not seeing any results? Or if you do see any results, what are they (and what value did you pass into the sub)?
Can you revert your usage of "Replace" to your original (as shown in ajackblackgoat's post) to: l = K.Remove(0, 5)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.