Results 1 to 5 of 5

Thread: Index Outside of Array Runtime error....

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Index Outside of Array Runtime error....

    I just recently started getting an index out of bounds error on one of my arrays at runtime. I was not having this issue previously. It only seems to happen when several items are added to a listbox. Here is my declaration and code. Thanks for any help resolving this issue.

    Code:
    Dim ClientActiveMedProbs(frmmain.lstActiveMedProbs.Items.Count) As String
    
      'store all active medical problem information in an array & build a string
            For i As Integer = 0 To frmmain.lstActiveMedProbs.Items.Count - 1
                ClientActiveMedProbs(i) = frmmain.lstActiveMedProbs.Items.Item(i).ToString
            Next
    
            If frmmain.lstActiveMedProbs.Items.Count = 0 Then
                ActiveMedProbsResult = ""
            Else
                ActiveMedProbsResult = String.Join(", ", ClientActiveMedProbs)
                ActiveMedProbsResult = Left(ActiveMedProbsResult, Len(ActiveMedProbsResult) - 2)
            End If
    The actual error message is:

    Index was outside the bounds of the array.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Index Outside of Array Runtime error....

    First up, this:
    Code:
    Dim ClientActiveMedProbs(frmmain.lstActiveMedProbs.Items.Count) As String
    should be this:
    Code:
    Dim ClientActiveMedProbs(frmmain.lstActiveMedProbs.Items.Count - 1) As String
    As for the problem, it looks as though it should be OK so, as always, the thing to do is to debug your code. Run the project in the debugger and step through that section. What is the Count of the ListBox Items? What is the Length of the array? What is 'i'? You don't solve run time errors just by looking at code. You watch it as it's running and take into account the data it's actually using at the time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Index Outside of Array Runtime error....

    Ok, I have continued to debug this and am still getting the error at runtime. I even tested in debug mode in VB and added 20 items to the listbox. No error in VB. Then I compiled it and it immediately errored with the same error message at runtime. I would like to emphasize that I am working with a listbox control here, not a listview.

    Code:
    Dim ClientActiveMedProbs(frmmain.lstActiveMedProbs.Items.Count -1) As String
    Dim i as Integer
    
      'store all active medical problem information in an array 
            For i = 0 To frmmain.lstActiveMedProbs.Items.Count - 1
                ClientActiveMedProbs(i) = frmmain.lstActiveMedProbs.Items.Item(i).ToString
            Next
    The issue is definitely a mathematical problem. The code in the loop is basically saying: From the index of 0 to the total count of items in the listbox -1, correct. This means that the listbox index will start at 0 and continue to loop through until it reaches the total count of items in the listbox - 1.

    I am missing something very basic here. Please help! Thanks.

    -Jeremy

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Index Outside of Array Runtime error....

    What is the actual value of i at the point at which it errors?

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Index Outside of Array Runtime error....

    if the error is happening on this line:
    ClientActiveMedProbs(i) = frmmain.lstActiveMedProbs.Items.Item(i).ToString

    Then what you need to do is determine WHICH side of the = the error is on....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width