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

Recommended Answers

All 2 Replies

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.