Hello i want to know how we can reallocate or refresh the array .In my program i have assign 100 of array's to send byte in each Function ,i am using 4 function in a loop by calling 2nd func in 1st ,3rd in 2nd ..., to get the value from Serial port .So, when i assign 100 of array ,the loop of each function working properly but when 100 of array over it stop 's the looping then i add more 50 to it, it working but if this also have over, then again i have to assign the array for it thus we can assign infinite no of array . IF any doubt in Question Pls ask.

Dani AI

Generated

Problem summary and quick direction: was hitting a capacity limit because a fixed-size byte array was used to accumulate serial data. Replies noted resizing and keeping existing contents and one suggested using a stream. Resizing a raw array repeatedly works but copies data every time and is fragile for continuous serial reads. A better, simpler pattern is to accumulate incoming bytes in a dynamic buffer and remove processed bytes as you go.

Practical recommendation (VB.NET): use System.Collections.Generic.List(Of Byte) or System.IO.MemoryStream and append each read from the port. Read into a small temporary buffer, then write that chunk into the dynamic buffer. Example pattern:

' VB.NET: accumulate bytes
Dim ms As New System.IO.MemoryStream()
Dim tmp(511) As Byte
Dim n As Integer = serialPort.Read(tmp, 0, tmp.Length)
If n > 0 Then
    ms.Write(tmp, 0, n)
End If
' process ms.ToArray() or ms.GetBuffer()

Notes and troubleshooting tips: SerialPort.DataReceived runs on a worker thread — protect shared buffers with SyncLock or marshal to the UI thread before touching UI controls. Avoid unbounded growth: scan for complete messages, remove processed bytes (keep an offset or copy remaining bytes periodically) or use a circular buffer when low-latency steady reads are required. For older VB6 code, use a streaming object (for example ADODB.Stream) or collect chunks in a Collection and combine when needed. For reference, see List(Of T), MemoryStream and SerialPort.Read.

Recommended Answers

All 4 Replies

you don't mean ReDim Array(100)

do you?

By assigning ReDim We have not to reallocate again and again

or u can use Redim Preserve Array(100) to avoid deleting the current value of an Array.

Instead of array why can't you try stream?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.