Managing Ports and Transfers in .NET
The Remove method can also remove selected characters:
' Remove the first two characters:
stringBuffer.Remove(0, 2)
// Remove the first two characters:
stringBuffer.Remove(0, 2);
You can specify a capacity when declaring a StringBuilder object:
Friend stringBuffer As New StringBuilder(1024)
internal StringBuilder stringBuffer = new StringBuilder(1024);
The StringBuilder object can store up to the specified capacity without having
to allocate more memory. On exceeding the specified capacity, the object grows
in size as needed to hold the data.
! $ +
For storing bytes and other data types as well as strings, the List(Of T) class (the
List class in C#) offers a solution. The class is in the System.Collections.Generic
namespace and supports methods for manipulating, searching, and sorting list
elements. The AddRange method can add the contents of an array to the end of
a list, expanding the list’s capacity as needed. The RemoveRange method can
remove a range of elements from a list. The Clear method removes all elements
in a list.
A List(Of T) object can hold a series of bytes:
Imports System.Collections.Generic
Friend portBuffer As New List(Of Byte)
using System.Collections.Generic;
internal List<Byte> portBuffer = new List<Byte>();