|
|||
|
Hi All,
I'm sure this is easy to answer but I can not figureit out. I am getting an OutOfBoundException when I run this loop that loops through the listbox in question and looks for duplicate values. Could anyone shed any light on this? For intListCountF As integer = 0 to frmPlayer.Listbox1.ListCount -1 For intListCountB As Integer = frmPlayer.Listbox1.Listcount -1 to 0 Step -1 if frmplayer.Listbox1.List(intListCountB) = frmPlayer.Listbox1.List(intListCountF) Then frmplayer.Listbox1.RemoveRow(intListCountB) end if Next Next Cheers, Liam. |
|
|
||||
|
||||
|
|
|
|||
|
Liam Whan <liam.whan@gmail.com> wrote in news:4fc21f6f$0$1762$c3e8da3
$92d0a893@news.astraweb.com: > Hi All, > > I'm sure this is easy to answer but I can not figureit out. > > I am getting an OutOfBoundException when I run this loop that loops > through the listbox in question and looks for duplicate values. Could > anyone shed any light on this? > > > For intListCountF As integer = 0 to frmPlayer.Listbox1.ListCount -1 > For intListCountB As Integer = frmPlayer.Listbox1.Listcount -1 to 0 > Step -1 > if frmplayer.Listbox1.List(intListCountB) = > frmPlayer.Listbox1.List(intListCountF) Then > frmplayer.Listbox1.RemoveRow(intListCountB) > end if > Next > Next > > Cheers, > Liam. > Because you're reducing the number of rows in the listbox while still indexing on the original number of rows. At some point you will try to access a row number that no longer exists in the listbox. -- Dale M. Arends dalearends@sbcglobal.net |
|
|||
|
Liam Whan wrote:
> I'm sure this is easy to answer but I can not figureit out. > > I am getting an OutOfBoundException when I run this loop that loops > through the listbox in question and looks for duplicate values. Could > anyone shed any light on this? > > > For intListCountF As integer = 0 to frmPlayer.Listbox1.ListCount -1 > For intListCountB As Integer = frmPlayer.Listbox1.Listcount -1 to 0 > Step -1 > if frmplayer.Listbox1.List(intListCountB) = > frmPlayer.Listbox1.List(intListCountF) Then > frmplayer.Listbox1.RemoveRow(intListCountB) > end if > Next > Next You're clearing your list. When intListCountF has the same value as intListCountB, you're removing the item. Since you will get to *every* value in the list in both For loops, you will remove *everything*. Eventually you're left with a list containing zero items. Try something like this (untested air code): For intListCountF As Integer = 0 To frmPlayer.Listbox1.ListCount - 2 For intListCountB As Integer = frmPlayer.Listbox1.ListCount - 1 To _ intListCountF + 1 Step -1 If frmplayer.Listbox1.List(intListCountB) = _ frmPlayer.Listbox1.List(intListCountF) Then frmplayer.Listbox1.RemoveRow(intListCountB) End If Next Next -- - Can we talk about this next week? - Just one more thing... - Is it next week already?! |
|
|||
|
On 28/05/2012 7:41 AM, Auric__ wrote:
> Liam Whan wrote: > >> I'm sure this is easy to answer but I can not figureit out. >> >> I am getting an OutOfBoundException when I run this loop that loops >> through the listbox in question and looks for duplicate values. Could >> anyone shed any light on this? >> >> >> For intListCountF As integer = 0 to frmPlayer.Listbox1.ListCount -1 >> For intListCountB As Integer = frmPlayer.Listbox1.Listcount -1 to 0 >> Step -1 >> if frmplayer.Listbox1.List(intListCountB) = >> frmPlayer.Listbox1.List(intListCountF) Then >> frmplayer.Listbox1.RemoveRow(intListCountB) >> end if >> Next >> Next > You're clearing your list. When intListCountF has the same value as > intListCountB, you're removing the item. Since you will get to *every* value > in the list in both For loops, you will remove *everything*. Eventually > you're left with a list containing zero items. > > Try something like this (untested air code): > > For intListCountF As Integer = 0 To frmPlayer.Listbox1.ListCount - 2 > For intListCountB As Integer = frmPlayer.Listbox1.ListCount - 1 To _ > intListCountF + 1 Step -1 > If frmplayer.Listbox1.List(intListCountB) = _ > frmPlayer.Listbox1.List(intListCountF) Then > frmplayer.Listbox1.RemoveRow(intListCountB) > End If > Next > Next > Of Course! I'm an idiot! Thanks so much guys. what I eventually went with was an algorithim that -1 off the row count every time it removed a row. Thanks again! Liam |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|