Solved

Index was outside the bounds of the array

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

20 Experts available now in Live!

Get 1:1 Help Now