Solved

Index was outside the bounds of the array

Posted on 2004-07-30
4
634 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Name Space error VS2015 1 47
VB.NET - Refactor Class per SOLID principles 2 40
UDP Data Capture Thread Issue 7 41
Read top line from CSV file 14 56
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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