Solved

Index was outside the bounds of the array

Posted on 2004-07-30
4
637 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

Industry Leaders: 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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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