1 Replies - 2227 Views - Last Post: 09 December 2012 - 08:15 AM Rate Topic: -----

#1 AllStarJam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-December 12

array error

Posted 08 December 2012 - 10:39 PM

I need this code to break up a string and not sure how to code it. I've played with it several different ways, and this was what I got. The error im getting is "Index was out of range. Must be non-negative and less than the size of the collection." at y = usrinput_.IndexOf("d", i). Here is all the code
 Function process_String(ByVal ps As String, ByVal d As String) As Integer()
       
        'gathers data and splits it, storing it in string array 
       

        'define(variables)
        Dim usrInput(4) As Integer
        Dim x As Integer = 0
        Dim i As Integer = 0
        Dim y As Integer = 0
        Dim usrinput_ As String

        'simplify input
        usrinput_ = ps
        'find comma and split numbers
        Do While x <= usrInput.GetUpperBound(0)
            y = usrinput_.IndexOf("d", i)
            If y = -1 Then
                y = usrinput_.Length
                usrInput(x) = CInt(ps.Substring(i, y - i))
                i = y + 1
                x += 1
                If i = usrInput.GetUpperBound(0) Then
                    ReDim Preserve usrInput(usrInput.GetUpperBound(0) + 1)
                End If
            End If
        Loop

        'return value
        Return (usrinput)



Is This A Good Question/Topic? 0
  • +

Replies To: array error

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5520
  • View blog
  • Posts: 11,829
  • Joined: 02-June 10

Re: array error

Posted 09 December 2012 - 08:15 AM

Arrays are zero indexed meaning an array of 10 items is indexed 0-9.
You cannot try to get index 10 because that is really element 11 of a 10 element array.

I cover it in more detail in this article:
What does this error message mean?[/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1