I have an array char[] buffer but I dont want to initialize it because I dont no how big its going to be. I have a simple StreamReader.Read() and I am processing each character, if the character is legit I add it to the buffer.
buffer[buffersize] = c;
buffersize++;
And if the char is a \r or \n thats a new line so I send the final buffer and process the command
if the char is 8 which is a backspace I want to delete the last character in the array.
buffersize--;
What I want to accomplish is that by subtracting 1 from the buffersize I want to delete the buffer[buffersize].
Any ideas am I missing something?
Loading
theLizardPosted May 19, 2011, 6:50 PM
char[] buffer = new char[0];
char c = '\n';
if (c != '\b') //only add the char if it is NOT the backspace, you can also test for other char values
{
Array.Resize(ref buffer, buffer.Length + 1);
buffer[buffer.Length - 1] = c;
}
To remove last element in array as you have indicated with buffersize-- do this with Array,Resize(ref buffer, buffer.length -1); but if you don;t put it in the array in the first place you wont need to remove it.
Sam HobbsPosted May 18, 2011, 10:06 PM
VulpesPosted May 18, 2011, 9:54 AM
In contrast the elements of a List
List
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
char c = (char)sr.Read();
if (c == '\r' || c == '\n') // EDITED
{
string command = new string(buffer.ToArray());
// process command
}
else if (c == '\b' && buffer.Count > 0)
{
buffer.RemoveAt(buffer.Count - 1);
}
else
{
buffer.Add(c);
}
}
}
Posted May 18, 2011, 3:25 AM
FroglegPosted May 18, 2011, 3:14 AM
using System.Collections;
ArrayList myAL = new ArrayList();
myAL.Add("Hello");
myAL.Add("World");