wadoyathink??

G

Guest

hey all,

below is the code i used to remove an item from a listbox on my .aspx,
please tell me what you think. Could i have done it any cleaner?

thanks,
rodchar
 
G

Guest

Here's the code:

Private Sub BtnRemove_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BtnRemove.Click
Dim lbItems As ListItem
Dim arrayListOfListItems As New ArrayList
For Each lbItems In LbLineDesc.Items
If lbItems.Selected Then
arrayListOfListItems.Add(lbItems)
End If
Next

For Each iItem As ListItem In arrayListOfListItems
LbLineDesc.Items.Remove(LbLineDesc.Items.FindByValue(iItem.Value))
Next
End Sub

please advise,
rodchar
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Mark said:
The ArrayList step isn't necessary - you already know which items are
selected, so just delete them.

If the OP didn't start new threads all the time, you would have seen the
error he got with the original code.

You can't delete items from a collection you are looping through.
 
K

Kevin Spencer

It's so clear I can't even see it!

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
M

Mark Rae

You can't delete items from a collection you are looping through.

Is this specifically a VB.NET limitation? The following works fine in C#:

for(int intItem = lstTest.Items.Count -1; intItem >= 0; intItem--)
{
if(lstTest.Items[intItem].Selected)
{
lstTest.Items.RemoveAt(intItem);
}
}
 
G

gerry

yes - but now you are not using an iterator ie. foreach



Mark Rae said:
You can't delete items from a collection you are looping through.

Is this specifically a VB.NET limitation? The following works fine in C#:

for(int intItem = lstTest.Items.Count -1; intItem >= 0; intItem--)
{
if(lstTest.Items[intItem].Selected)
{
lstTest.Items.RemoveAt(intItem);
}
}
 
G

Guest

Actually this worked for me on the vb side:

Dim intItem As Integer
For intItem = LbLineDesc.Items.Count - 1 To 0 Step -1
If LbLineDesc.Items(intItem).Selected Then
LbLineDesc.Items.RemoveAt(intItem)
End If
Next intItem

Can someone explain why this works and the for each doesn't please?

Mark Rae said:
You can't delete items from a collection you are looping through.

Is this specifically a VB.NET limitation? The following works fine in C#:

for(int intItem = lstTest.Items.Count -1; intItem >= 0; intItem--)
{
if(lstTest.Items[intItem].Selected)
{
lstTest.Items.RemoveAt(intItem);
}
}
 
G

Guest

rodchar said:
Actually this worked for me on the vb side:

Dim intItem As Integer
For intItem = LbLineDesc.Items.Count - 1 To 0 Step -1
If LbLineDesc.Items(intItem).Selected Then
LbLineDesc.Items.RemoveAt(intItem)
End If
Next intItem

Can someone explain why this works and the for each doesn't please?

Mark Rae said:
You can't delete items from a collection you are looping through.

Is this specifically a VB.NET limitation? The following works fine in C#:

for(int intItem = lstTest.Items.Count -1; intItem >= 0; intItem--)
{
if(lstTest.Items[intItem].Selected)
{
lstTest.Items.RemoveAt(intItem);
}
}
 
G

Guest

When you are using an enumerator (as foreach does) to loop through a
collection, you can't change the collection. That's just a limitation in
the defintion of how an enumerator works, to keep the code in the
enumerators to get awfully complicated, bloated and slow.
Actually this worked for me on the vb side:

Dim intItem As Integer
For intItem = LbLineDesc.Items.Count - 1 To 0 Step -1
If LbLineDesc.Items(intItem).Selected Then
LbLineDesc.Items.RemoveAt(intItem)
End If
Next intItem

Can someone explain why this works and the for each doesn't please?

Mark Rae said:
You can't delete items from a collection you are looping through.
Is this specifically a VB.NET limitation? The following works fine in C#:

for(int intItem = lstTest.Items.Count -1; intItem >= 0; intItem--)
{
if(lstTest.Items[intItem].Selected)
{
lstTest.Items.RemoveAt(intItem);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top