Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: blinkdude on December 02, 2003, 03:35 AM

Title: ?out of bounds
Post by: blinkdude on December 02, 2003, 03:35 AM
//checks to see if Username is in listview and if it is it deletes it, i get a Index out of bounds error sometimes ? ideas?


For X = 1 To Form1.lstls.ListItems.Count
 If Form1.lstls.ListItems.Item(X).Text = Username Then
   Form1.lstls.ListItems.Remove X
 End If
Next X
Title: Re:?out of bounds
Post by: DEAD on December 02, 2003, 06:26 AM

lstls.ListItems.Remove lstls.ListItems(x).Index
Title: Re:?out of bounds
Post by: ______ on December 02, 2003, 08:36 AM
Quote from: blinkdude on December 02, 2003, 03:35 AM
//checks to see if Username is in listview and if it is it deletes it, i get a Index out of bounds error sometimes ? ideas?


For X = 1 To Form1.lstls.ListItems.Count
 If Form1.lstls.ListItems.Item(X).Text = Username Then
   Form1.lstls.ListItems.Remove X
 End If
Next X



Lets say you had five users in your listview, and you remove one the code you show is still checking the amount before you removed the user. so its still checking 5 instead of 4.
Maybe this will help?

For i = 1 To Form1.lstls.ListItems.Count
If username = Form1.lstls.ListItems(i).Text Then
Form1.lstls.ListItems.Remove Form1.lstls.FindItem(username).Index
Exit For
End If
Next i


Title: Re:?out of bounds
Post by: blinkdude on December 02, 2003, 02:56 PM
thanks works!
Title: Re:?out of bounds
Post by: MyndFyre on December 02, 2003, 03:39 PM
In the future, you might want to post this question on the Visual Basic forum, as it really has nothing to do (except perhaps with your own project) with Battle.net bot development.
Title: Re:?out of bounds
Post by: ObsidianWolf on December 02, 2003, 10:35 PM
or perhaps one could search the forum for listbox or listview?  I know there is tons of example on removing items in listboxes(well maybe only more then 10 examples, but still).

Title: Re:?out of bounds
Post by: Puzzle on December 03, 2003, 12:16 PM
I always use GoTo or Exit Sub when I am removing an item from a listview within a  loop.
Title: Re:?out of bounds
Post by: Grok on December 03, 2003, 12:32 PM
Quote from: Puzzle on December 03, 2003, 12:16 PM
I always use GoTo or Exit Sub when I am removing an item from a listview within a  loop.

Why?  Give an example.
Title: Re:?out of bounds
Post by: Spht on December 03, 2003, 01:35 PM
It sounds like you're all searching through the list in forward order without fixing your loop index after you remove an item, which is causing it to go out of bounds when you reach the last item.

I suggest either fixing your index after removing an item (i = i - 1) or search through the list in reverse order so that the removed indexes don't interfere with your search.
Title: Re:?out of bounds
Post by: Puzzle on December 03, 2003, 02:35 PM
Quote from: Grok on December 03, 2003, 12:32 PM
Quote from: Puzzle on December 03, 2003, 12:16 PM
I always use GoTo or Exit Sub when I am removing an item from a listview within a  loop.

Why?  Give an example.

Continueing the loop = wasted CPU cycles.
Title: Re:?out of bounds
Post by: Grok on December 03, 2003, 02:48 PM
Quote from: Puzzle on December 03, 2003, 02:35 PM
Quote from: Grok on December 03, 2003, 12:32 PM
Quote from: Puzzle on December 03, 2003, 12:16 PM
I always use GoTo or Exit Sub when I am removing an item from a listview within a  loop.

Why?  Give an example.

Continueing the loop = wasted CPU cycles.

Give a code example.
Title: Re:?out of bounds
Post by: Spht on December 03, 2003, 03:14 PM
Quote from: Puzzle on December 03, 2003, 02:35 PM
Quote from: Grok on December 03, 2003, 12:32 PM
Quote from: Puzzle on December 03, 2003, 12:16 PM
I always use GoTo or Exit Sub when I am removing an item from a listview within a  loop.

Why?  Give an example.

Continueing the loop = wasted CPU cycles.

Not when continuing the loop is necessary for complete accuracy (there may be more than one matching item which you need to remove).
Title: Re:?out of bounds
Post by: Adron on December 05, 2003, 03:55 AM
Quote from: Puzzle on December 03, 2003, 12:16 PM
I always use GoTo or Exit Sub when I am removing an item from a listview within a  loop.

Why do you never use Exit Do or Exit For?