Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

non-negative and less than the size of the collection

Posted on 2011-09-29
2
Medium Priority
?
313 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

773 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