Solved

non-negative and less than the size of the collection

Posted on 2011-09-29
2
305 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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now