3 Replies - 731 Views - Last Post: 01 April 2012 - 06:22 AM Rate Topic: -----

#1 znerihc  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 05-February 12

Index Out of Range Error

Posted 31 March 2012 - 11:27 PM

Can somebody please tell me how this code generates an index out of range error? In my mind I am thinking that this should not, but then, I have read somewhere that a programmer should always trust his tools, so there must really be something wrong with this code. Any help?


foreach (ListViewItem lv in listView1.CheckedItems)
            {
                Point temp = new Point(int.Parse(listView1.CheckedItems[lv.Index].SubItems[0].Text), int.Parse(listView1.CheckedItems[lv.Index].SubItems[1].Text));
                figPoints.Remove(temp);
                lv.Remove();
            }


This post has been edited by znerihc: 31 March 2012 - 11:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Index Out of Range Error

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Index Out of Range Error

Posted 01 April 2012 - 03:41 AM

lv.Index returns the index of the ListViewItem within the given list view. Since the list view probably contains more items than checked items, lv.Index is usually not a valid index for CheckedItems. Let's consider this example:

The list view contains three items. Let's call them Foo, Bar and Baz. Baz is the only item that is checked. So listView1.CheckedItems returns a collection containing Baz. Since Bar is the third item in the list view, its index is 2. So if you do listView.CheckedItems[lv.Index] (lv being Baz), you're doing listView.CheckedItems[2]. But since CheckedItems only contains one element, this causes an IndexOutOfBounds exception.

I also have to wonder why you even try to use the index to get at the ListViewItem. You already have the ListViewItem - that's where you get the index from. I mean, why don't you just use lv.SubItems?
Was This Post Helpful? 3
  • +
  • -

#3 znerihc  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 05-February 12

Re: Index Out of Range Error

Posted 01 April 2012 - 04:32 AM

View Postsepp2k, on 01 April 2012 - 03:41 AM, said:

I also have to wonder why you even try to use the index to get at the ListViewItem. You already have the ListViewItem - that's where you get the index from. I mean, why don't you just use lv.SubItems?


^ This. Is why I felt so dumb after reading your reply. Why didn't I think of that? It could have made my life so much easier. Thanks man:) Really great help. Will keep that in mind in case I have to do something like this again.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,649
  • Joined: 02-June 10

Re: Index Out of Range Error

Posted 01 April 2012 - 06:22 AM

What does this error message mean?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1