Saturday, March 24, 2012

wadoyathink?

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,
rodcharHere'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.FindByVal ue(iItem.Value))
Next
End Sub

please advise,
rodchar

"rodchar" wrote:

> 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
"rodchar" <rodchar@.discussions.microsoft.com> wrote in message
news:626A6853-F2C4-4861-94A3-FD6295343C8C@.microsoft.com...

>Could i have done it any cleaner?

The ArrayList step isn't necessary - you already know which items are
selected, so just delete them.
Mark Rae wrote:
> "rodchar" <rodchar@.discussions.microsoft.com> wrote in message
> news:626A6853-F2C4-4861-94A3-FD6295343C8C@.microsoft.com...
>> Could i have done it any cleaner?
> 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.
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.

"rodchar" <rodchar@.discussions.microsoft.com> wrote in message
news:157BFE5A-0F32-4B6B-9C6E-F0CB818DF30C@.microsoft.com...
> 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
"Gran Andersson" <guffa@.guffa.com> wrote in message
news:u%23uxe8pbGHA.3320@.TK2MSFTNGP04.phx.gbl...

> 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);
}
}
yes - but now you are not using an iterator ie. foreach

"Mark Rae" <mark@.markN-O-S-P-A-M.co.uk> wrote in message
news:Od6qlhqbGHA.1960@.TK2MSFTNGP05.phx.gbl...
> "Gran Andersson" <guffa@.guffa.com> wrote in message
> news:u%23uxe8pbGHA.3320@.TK2MSFTNGP04.phx.gbl...
> > 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);
> }
> }
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" wrote:

> "G?ran Andersson" <guffa@.guffa.com> wrote in message
> news:u%23uxe8pbGHA.3320@.TK2MSFTNGP04.phx.gbl...
> > 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);
> }
> }
>

"rodchar" wrote:

> 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" wrote:
> > "G?ran Andersson" <guffa@.guffa.com> wrote in message
> > news:u%23uxe8pbGHA.3320@.TK2MSFTNGP04.phx.gbl...
> > > 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);
> > }
> > }
thanks everyone for the help. this has been very productive.

"rodchar" wrote:

> 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
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.

rodchar wrote:
> 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" wrote:
>> "G?ran Andersson" <guffa@.guffa.com> wrote in message
>> news:u%23uxe8pbGHA.3320@.TK2MSFTNGP04.phx.gbl...
>>
>>> 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);
>> }
>> }
>>
>>
>>

0 comments:

Post a Comment