Chapter 10
To reduce the number of resizing operations needed as elements are added to
the list, you can specify an initial capacity when creating the list object:Friend portBuffer As New List(Of Byte)(1024)
internal List
The code below appends received bytes to the end of a list and calls a routine to
process the data. The code can execute in a DataReceived event or Timer event:Dim numberOfBytesToRead As Integer
' Get the number of bytes available to read.numberOfBytesToRead = myComPort.BytesToRead' Create a byte array large enough to hold the bytes to be read.Dim newReceivedData(numberOfBytesToRead - 1) As Byte' Read the bytes into the byte array.myComPort.Read(newReceivedData, 0, numberOfBytesToRead)' Add the bytes to the end of the list.portBuffer.AddRange(newReceivedData)' Call a routine to process the data.ProcessData()