Solved

Index was outside the bounds of the array

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now