?
Solved

non-negative and less than the size of the collection

Posted on 2011-09-29
2
Medium Priority
?
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 1000 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 1000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

719 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