I am building an application for my brother that takes string input form an external source, GPS plotter, which I would like to display in a multiline textbox. If someone were to leave this application running for an extended period I don't want the TextBox.MaxLength to be exceeded or memory allocation to become excessive.
I can't think of a good way to implement this. I need to display the last n rows in a textbox.
Edit: The marked solution is correct. Thanks Jon. I ended up making it generic. Below is what I used.
public class ArrayBuffer<T>
{
private readonly int _maxLines;
private int _writePosition;
private readonly T[] _buffer;
public ArrayBuffer(int maxLines = 100)
{
_maxLines = maxLines;
_buffer = new T[_maxLines];
}
public T[] Push(T value)
{
_buffer[_writePosition++] = value;
_writePosition %= _maxLines;
return _buffer.Skip(_writePosition)
.Concat(_buffer.Take(_writePosition))
.Where(line => line != null).ToArray();
}
}
Usage:
var myArrayBuffer = new ArrayBuffer<string>(50);
string[] bufferedStringArray = myArrayBuffer.Push("some string");