Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i run the code and after going through two forms i select item from one list box and another item from another listbox and click on land button. after that i get an error saying "Collection index must be in the range 1 to the size of the collection."

Private Sub btn_land_plane_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_land_plane.Click<br />
<br />
        Dim runwayindexno As Integer<br />
        Dim runwayfrmselected As Form<br />
        runwayindexno = lst_runways.SelectedIndex<br />
        runwayfrmselected = runway(runwayindexno)<br />
<br />
    End Sub

It highlight [==runway(runwayindexno)] and displays the error.

i have looked for the solution cant find one that relates to my code. i don't know how much code
to include if need i can post.
if you need any more information please ask and any help is much appreciated.
[no name] 22-May-14 11:30am    
No code needed. Debug your application and see if runwayindexno is within the bounds of your runway collection.

The problem surely lies in the way VB.NET is handling array indices.
Obviously, in your .vb file there must be a
Option Base 1


This has the effect of indexing arrays from 1 to Count.
Whereas the SelectedIndex of a ListBox returns a value between 0 and (Count - 1) (which corresponds to Option Base 0 in VB.NET).

You then have two alternatives :
- OR you set Option Base 0
- OR you convert every index obtained by adding it 1

The former is probably the best one, as this will lead to a kind of consistency with array indices in your application.
Share this answer
This occurs because runwayindexno is outside the index of the array runway. I think you're trying to do retrieve index from a list box and perform some operation.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900