locked
Index was outside the bounds of the array?

    Question

  • Hello,

    When I execute, VB gives me an error stating that variable tempArrayNEL's (at location in bold and italicized) index is outside the bounds of the array. I can't figure out why. Please help. Thanks.

     

     

        Dim counter As Integer
        Dim nelc As Integer = 0 <br/>
        Dim tempArray() As String<br/>
        Dim tempArrayNEL() As String = {} <br/>
    <br/>
        tempArray = TextBox1.Lines<br/>
    <br/>
        For counter = 0 To tempArray.GetUpperBound(0)<br/>
          If String.IsNullOrEmpty(tempArray(counter)) = False Then<br/>
            <span style="text-decoration:underline"><em><strong>tempArrayNEL</strong>
    </em>
    </span>
    (nelc) = tempArray(counter)<br/>
            nelc = nelc + 1<br/>
          End If<br/>
        Next
    

     

     

    Thursday, December 30, 2010 6:53 PM

All replies

  • It is always helpful to have a short description of what the code is supposed to do.  I am guessing - remove blank lines, which is what my response is:

     

            Dim foo As New List(Of String)
            foo.AddRange(TextBox1.Lines)

            TextBox1.Lines = foo.Where(Function(n) n <> "").ToArray


    Subnet Calculator / Planner      Serial Port      Random
    Thursday, December 30, 2010 7:20 PM
  • Hi,

    Here is a sample that will collect all the lines that aren't blank and put them into an array:

    Dim stringList As New List(Of String)()
    For Each s As String In TextBox1.Lines
      If Not String.IsNullOrEmpty(s) Then
        stringList.Add(s)
      End If
    Next
    
    Dim strings As String() = stringList.ToArray()
    

    Goldfish64 - Please remember to mark any replies as answers if they help and unmark them if they provide no help.
    Thursday, December 30, 2010 7:38 PM
  • Hi JamesBund008

    Index was outside the bounds of the array

    Make sure that the maximum index on a list is less than the list size
    The maximum index on a list must be less than the list size.

    Make sure the index is not a negative number.
    This exception will be thrown if the index is less than zero.

    One way to check is to use the Step Into to see what value you get.


    For further study please check this link. Troubleshooting Exceptions: System.IndexOutOfRangeException
    http://msdn.microsoft.com/en-us/library/3zwz4dx3.aspx

     


    Be nice and courteous always, with the measure you use, it will be measured to you. Give a person a fish, and you feed him/her for a day. Teach a person to fish, and you feed him/her for life. Thank you for your time and help. We always be greatful.
    • Proposed as answer by Bin-ze Zhao Monday, January 03, 2011 6:04 AM
    • Marked as answer by Calvin_GaoModerator Thursday, January 06, 2011 6:15 AM
    • Unmarked as answer by JamesBund008 Monday, January 10, 2011 1:01 AM
    • Unproposed as answer by JamesBund008 Monday, January 10, 2011 1:02 AM
    Thursday, December 30, 2010 9:32 PM
  • a good reason to use List(Of T) instead of arrays. And because you are not specifying the array dimension when instantiated, you will run into other problems. You have to ReDim your array with each loop and if you don't use Preserve you will lose all the previous array elements.

    I changed your code a little (got rid of the tempArray - no need for this since the TextBox Lines is already a collection), and used the Trim method on each line since a single space is not a Null or Empty value.

      Dim nelc As Integer = 0
      Dim tempArrayNEL() As String = {}
      For counter As Integer = 0 To TextBox1.Lines.Count - 1
       If Not String.IsNullOrEmpty(TextBox1.Lines(counter).Trim) Then
        ReDim Preserve tempArrayNEL(nelc)
        tempArrayNEL(nelc) = TextBox1.Lines(counter)
        nelc += 1
       End If
      Next
    

     

    • Proposed as answer by Bin-ze Zhao Monday, January 03, 2011 6:04 AM
    • Marked as answer by Calvin_GaoModerator Thursday, January 06, 2011 6:14 AM
    • Unmarked as answer by JamesBund008 Monday, January 10, 2011 1:01 AM
    • Unproposed as answer by JamesBund008 Monday, January 10, 2011 1:01 AM
    Thursday, December 30, 2010 10:14 PM
  •  Dim counter As Integer
    
     Dim nelc As Integer = 0 <br/>
    
     Dim tempArray() As String<br/>
    
     Dim tempArrayNEL() As String = {} <br/>
    
    <br/>
    
     tempArray = TextBox1.Lines<br/>
    
    <br/>
    
     For counter = 0 To tempArray.GetUpperBound(0)<br/>
    
      If String.IsNullOrEmpty(tempArray(counter)) = False Then<br/>
    
      <span style="text-decoration:underline"><em><strong>tempArrayNEL</strong>
    
    </em>
    
    </span>
    
    (nelc) = tempArray(counter)<br/>
    
      nelc = nelc + 1<br/>
    
      End If<br/>
    
     Next
    
    

     

     

    the reason it dont work is that you have created an array of lenght 0 . So this array have no lenght

    when you are  coding TemparrayNEL(0), you are refering to the first data place in this array, but since it has no lenght, this first place does not exists. So you are getting an error (Basicaly, the first data place is after the end of an array of lenght zero)

     

    try to work with lists instead of arrays, it is much easier

    • Proposed as answer by Martin_XieModerator Thursday, January 06, 2011 1:19 AM
    • Marked as answer by Calvin_GaoModerator Thursday, January 06, 2011 6:14 AM
    • Unmarked as answer by JamesBund008 Monday, January 10, 2011 1:03 AM
    • Marked as answer by JamesBund008 Monday, January 10, 2011 1:03 AM
    • Unmarked as answer by JamesBund008 Sunday, January 16, 2011 2:25 AM
    • Unproposed as answer by JamesBund008 Sunday, January 16, 2011 2:25 AM
    • Marked as answer by JamesBund008 Sunday, January 16, 2011 2:26 AM
    • Unmarked as answer by JamesBund008 Sunday, January 16, 2011 2:26 AM
    Friday, December 31, 2010 12:04 AM
  • Am I the only one shocked that this forum has a hard time with posting code?  Compared to the other forums I frequent this is the worst.  Microsoft should look at some of the non-Microsoft products ;)

    Subnet Calculator / Planner      Serial Port      Random
    Friday, December 31, 2010 12:08 AM
  • agreed 100% ---

    I start to be good to read code 1 word per line in super small character

    Friday, December 31, 2010 12:23 AM
  • My experience is that the first time you post code it looks ok, but woe be you if you attempt to edit the post later!!  Their code tool doesn't work correctly.
    Monday, January 03, 2011 6:17 PM
  • Am I the only one shocked that this forum has a hard time with posting code?  Compared to the other forums I frequent this is the worst.  Microsoft should look at some of the non-Microsoft products ;)


    Subnet Calculator / Planner      Serial Port      Random

     

    agreed 100% ---

    I start to be good to read code 1 word per line in super small character

     

    My experience is that the first time you post code it looks ok, but woe be you if you attempt to edit the post later!!  Their code tool doesn't work correctly.

     

    Hi,

     

    Thank for your valuable feedback, it is very useful to make a convenient and efficient forum!

    You can consider submitting your feedback to this forum:

    Forums Issues (not product support)

     

    Best Regards,


    Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 06, 2011 6:34 AM
  • How are any of these questions marked as an answer when the OP only posted once???????????????????????????????????????
    Subnet Calculator / Planner      Serial Port      Random
    Thursday, January 06, 2011 12:42 PM
  • Thanks everyone for their input. Some of the proposed solutions were unfamiliar to me since I had not advanced that far in VB.Net. Anyway, I had solved my problem. I used a "ReDim Preserve" after the 'IsNullOrEmpty' to fix the error.
    Monday, January 10, 2011 1:14 AM
  • so if you used Redim Preserve, how does Crazypennie's post answer your question?

    I don't see that mentioned in her post. I believe it was mentioned in mine.

    Monday, January 10, 2011 1:30 AM