Solved

non-negative and less than the size of the collection

Posted on 2011-09-29
2
309 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 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