Solved

non-negative and less than the size of the collection

Posted on 2011-09-29
2
308 Views
Last Modified: 2012-05-12
Hi There,

When the loop starts, I got the following error.
"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"


Private Shared MAXLENGTH As Integer = 94
Public Shared SPACE As Byte = CByte(&H20)


Private strBuilder As StringBuilder

Me.strBuilder = New StringBuilder(Record.MAXLENGTH)

            For i = 0 To Me.strBuilder.Capacity - 1
                Me.strBuilder(i) = ChrW(Record.SPACE)
            Next


Please advice.

Thanks.
0
Comment
Question by:theartha
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
ID: 36815349
Please try the following:

Private Shared MAXLENGTH As Integer = 94
Public Shared SPACE As Byte = CByte(&H20)


Private strBuilder As StringBuilder

Me.strBuilder = New StringBuilder(String.Empty.PadRight(MAXLENGTH), MAXLENGTH)

Open in new window


StringBuilder Constructor (String, Int32)
http://msdn.microsoft.com/en-us/library/zb91weab.aspx

String.PadRight (Int32)
http://msdn.microsoft.com/en-us/library/34d75d7s.aspx
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 250 total points
ID: 36815641
Your StringBuilder is empty.

Me.strBuilder(i) is a shorthand for Me.strBuilder.Chars(i).

Since there is no text in the StringBuilder, there is no Chars, and you cannot reference the first character. If you simply want to fill it in to work in it later, you can use the following:

Me.strBuilder = New StringBuilder(New String(" "c, MAXLENGTH),Record.MAXLENGTH)

If you want to fill it with something else than spaces, use it's Append method. to build it.

If all you only want a string of a given length, use the following String constructor instead:

Dim s As  New String(" "c, MAXLENGTH)

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
.NET tools for adding thread safety to a web app? 3 48
Get the selected ValueMember of Combobox 5 40
Disable extension 8 48
VB.net Open video relating to control 2 20
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question