ArrayList 'Index out of range'


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: ArrayList 'Index out of range'

  1. #1
    Miroslav St. Jeliaskoff Guest

    ArrayList 'Index out of range'

    Hi Pros,
    My problem this time is:
    I have a ListBox and an ArrayList, containing a class User as elements of the ArrayList.

    'in Declarations
    Private Users As New ArrayList() 'Array of all users
    Private dbRow as OleDb.DataRow


    'So I load all items from the DB:
    For Each dbRow In dbDataSet.Tables("Users").Rows
    Users.Add(New User(dbRow("UserName"), _
    dbRow("UserPass"), _
    dbRow("FullName"), _
    dbRow("Position"), _
    dbRow("AccessLevel")))
    Next dbRow

    lstUsers.DataSource = Users
    lstUsers.DisplayMember = "UserName"

    'So far everithing is OK. But when I remove an item from the Users ...

    Users.Remove(lstUsers.SelectedItem)
    lstUsers.DataSource = Users
    lstUsers.DisplayMember = "UserName" 'to show the changes in the ListBox

    '...and it goes away from the ListBox, when I click an item in it an exception raises saying: "Index was out of range. Must be non-negative and less than the size of the collection." (in mscorlib.dll) and the pointer in the Dev. environment (text editor) points to the first line in the code:

    Public Class frmUserAdmin

    , which is not of much help. This happens after the lstUsers.SelectedIndexChanged event is completed.

    Any ideas what I'm doing wrong and which index is out of range (i think the ArrayList's but how to check this index and it's setting)?

    Thanks,
    Miroslav St. Jeliaskoff

  2. #2
    Miroslav St. Jeliaskoff Guest

    Re: ArrayList 'Index out of range'

    One more thing I found out. When I remove the last element of the ArrayList then this exception occures. It doesn't metter which one is the last element.

    Miroslav
    "Miroslav St. Jeliaskoff" <miroslav@earthling.net> wrote in message news:3db03b07@tnews.web.devx.com...
    Hi Pros,
    My problem this time is:
    I have a ListBox and an ArrayList, containing a class User as elements of the ArrayList.

    'in Declarations
    Private Users As New ArrayList() 'Array of all users
    Private dbRow as OleDb.DataRow


    'So I load all items from the DB:
    For Each dbRow In dbDataSet.Tables("Users").Rows
    Users.Add(New User(dbRow("UserName"), _
    dbRow("UserPass"), _
    dbRow("FullName"), _
    dbRow("Position"), _
    dbRow("AccessLevel")))
    Next dbRow

    lstUsers.DataSource = Users
    lstUsers.DisplayMember = "UserName"

    'So far everithing is OK. But when I remove an item from the Users ...

    Users.Remove(lstUsers.SelectedItem)
    lstUsers.DataSource = Users
    lstUsers.DisplayMember = "UserName" 'to show the changes in the ListBox

    '...and it goes away from the ListBox, when I click an item in it an exception raises saying: "Index was out of range. Must be non-negative and less than the size of the collection." (in mscorlib.dll) and the pointer in the Dev. environment (text editor) points to the first line in the code:

    Public Class frmUserAdmin

    , which is not of much help. This happens after the lstUsers.SelectedIndexChanged event is completed.

    Any ideas what I'm doing wrong and which index is out of range (i think the ArrayList's but how to check this index and it's setting)?

    Thanks,
    Miroslav St. Jeliaskoff

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center