Solved

Index was outside the bounds of the array

Posted on 2004-07-30
4
631 Views
Last Modified: 2010-04-23
When using the following code and deleting the last Item in a ComboBox I get a System.IndexOutOfRangeException error

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        Dim intCount, intTempCount, I As Integer
        intCount = UBound(LastName)
        intTempCount = intCount - 1
        Dim TempFirst(intTempCount) As String, TempLast(intTempCount) As String, _
        TempEmail(intTempCount) As String, TempPhone(intTempCount) As String
        For I = 0 To intCount

            If cboPerson.Text = LastName(I) Then

            Else
                TempFirst(intTempCount) = FirstName(I)
                TempLast(intTempCount) = LastName(I)
                TempEmail(intTempCount) = Email(I)
                TempPhone(intTempCount) = Phone(I)
                I = I + 1
            End If

        Next
        For I = 0 To intTempCount
        Next
        FirstName(I) = TempFirst(intTempCount) ' This is were the Debugger catches the error
        LastName(I) = TempLast(intTempCount)
        Email(I) = TempEmail(intTempCount)
        Phone(I) = TempPhone(intTempCount)
        cboPerson.Items.RemoveAt(I)
        txtFirstName.Clear()
        txtLastName.Clear()
        txtEmail.Clear()
        txtPhoneNumber.Clear()
        If cboPerson.Items.Count = 0 Then
            txtLastName.Enabled = False
            txtFirstName.Enabled = False
            txtEmail.Enabled = False
            txtPhoneNumber.Enabled = False
            btnSave.Enabled = False
            btnClear.Enabled = False
            btnClose.Enabled = False
            mnuFileClose.Enabled = False
            mnuFileSave.Enabled = False
            mnuClear.Enabled = False
        End If
0
Comment
Question by:rebels18
4 Comments
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 75 total points
ID: 11682922
Check your For loop...

       For I = 0 To intCount
            If cboPerson.Text = LastName(I) Then
            Else
                TempFirst(intTempCount) = FirstName(I)
                TempLast(intTempCount) = LastName(I)
                TempEmail(intTempCount) = Email(I)
                TempPhone(intTempCount) = Phone(I)
                I = I + 1         'Here you are incrementing "I" again... are you skipping some index
            End If
        Next
        FirstName(I) = TempFirst(intTempCount) ' This is were the Debugger catches the error
              'Here you are already out of the For Loop. Index "I" is no good here.
-Baan
0
 
LVL 4

Expert Comment

by:SSSoftware
ID: 11683986
Additional comments on the data in a list style object

Explination:
The indexs of items in a combo or list box are dynamic (the same as a collection)

Therefore when you delete index #3 the item with index #4 shifts down to the index #3 spot.

This can cause missed (skipped) items if you are using a for next loop to look for items.


Suggestion:
When deleating items in a list (of any kind that shifts indexes) always work from the top (last) item in the list to the first. This way you will be sure of looking at all items.


For I = CBO.item.count-1 to 0 step -1
   ...
   ...
   ...
Next I

Good Luck,
Ed
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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