Hi,
I'm having trouble with 'stringBuilder'. this the snippet of code:

using (StreamReader sr = new StreamReader("file.txt"))
                {
                    word = sr.ReadLine();
                    listBox1.Items.Add(word.ToString());
                    label1.Text = word.Length.ToString();
                    StringBuilder sb = wr.GetStringBuilder();
                    sb.Remove(0,word.Length);
                }

it gives this error:
"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
I have tried all sorts of things, and I realise that the computer thinks that it is a negative number (which it isn't). Any help would be much appreciated.
Thanks

Hi

The problem is about your sb. You must fill the sb with a string value. In your code if you look your sb, it is lenth zero and it is empty. You can use like that.

using (StreamReader sr = new StreamReader("file.txt"))
            {
                StringWriter wr = new StringWriter();
                string  word = sr.ReadLine();
                listBox1.Items.Add(word.ToString());
                label1.Text = word.Length.ToString();
                //StringBuilder sb = wr.GetStringBuilder();
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(word);
                sb.Remove(0, word.Length);
            }

Hi,
I'm having trouble with 'stringBuilder'. this the snippet of code:

using (StreamReader sr = new StreamReader("file.txt"))
                {
                    word = sr.ReadLine();
                    listBox1.Items.Add(word.ToString());
                    label1.Text = word.Length.ToString();
                    StringBuilder sb = wr.GetStringBuilder();
                    sb.Remove(0,word.Length);
                }

it gives this error:
"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
I have tried all sorts of things, and I realise that the computer thinks that it is a negative number (which it isn't). Any help would be much appreciated.
Thanks

This question has already been answered. Start a new discussion instead.