Removing items from an arraylist

D

David Lozzi

Howdy,

I have a shopping cart in my arraylist. Works great for adding items but I'm
displaying the shopping cart in a gridview and the user can update the qty
of the items. If they set it to 0, then I want to remove it. I'm doing the
following script:

For Each ele In bag

Dim qty As Integer = CType(gvBag.Rows.Item(cnt).FindControl("txtQty"),
TextBox).Text

If qty > 0 Then

ele.Quantity = qty

Else

bag.Remove(ele)

End If

cnt += 1

Next

This will remove the item from the shopping cart, but then when the Next
loops, the elements have issues because one was removed. Can I reinitialise
the bag arraylist after the remove? I also tried "for i as integer = 0 to
bag.count - 1" but I got the similar issues: after I removed the item, the
total of the bag differed from the original, but the loop kept going....

Thanks,
 
M

Morten Wennevik

Hi David,

Can you show us your for loop as that should work.
The samples below are in C#, but they should work equally well in vb.net

Using an arraylist of numbers 12-19, the number 15 is removed from the
list.

for(int i = 0; i < list.Count; i++)
{
if (list.ToString() == "15")
list.RemoveAt(i);
}

for(int i = 0; i <= list.Count - 1; i++)
{
object o = list;
if (o.ToString() == "15")
list.Remove(o);
}
 
G

Guest

David,

You can do this by iterating the array list from top to bottom. something
like this

for (i=arr.length; i >= 0, i--)
{
....
}

Regards,
Augustin
 
D

David Lozzi

Here's the other loop I tried:

For i As Integer = 0 To bag.Count - 1

Dim item As BagItem = bag(i)

Dim qty As Integer = CType(gvBag.Rows.Item(i).FindControl("txtQty"),
TextBox).Text

If qty > 0 Then

item.Quantity = qty

Else

bag.RemoveAt(i)

End If

Next


--
David Lozzi
dlozzi@(remove)delphi-ts.com
www.delphi-ts.com


Hi David,

Can you show us your for loop as that should work.
The samples below are in C#, but they should work equally well in vb.net

Using an arraylist of numbers 12-19, the number 15 is removed from the
list.

for(int i = 0; i < list.Count; i++)
{
if (list.ToString() == "15")
list.RemoveAt(i);
}

for(int i = 0; i <= list.Count - 1; i++)
{
object o = list;
if (o.ToString() == "15")
list.Remove(o);
}
 
M

Morten Wennevik

What error do you get?

Are you certain the text in the control can be cast to TextBox? Or that
there will be a txtQty control at that position? And if so, that the Text
can be stored as Integer?

You might want to turn on Option Strict and Option Explicit. You will
probably get a few compiler error, but once those are sorted out you
should get more stable code.
 
D

David Lozzi

Has nothing to do with the control... The item does end up removing but it
appears the loop trys to go for another turn but the final count has been
changed so there is no final item. Getting this error:


Index was out of range. Must be non-negative and less than the size of the
collection.
Parameter name: index
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of
range. Must be non-negative and less than the size of the collection.
Parameter name: index


on line:
Line 34: Dim item As BagItem = bag(i)

--
David Lozzi
dlozzi@(remove)delphi-ts.com
www.delphi-ts.com


What error do you get?

Are you certain the text in the control can be cast to TextBox? Or that
there will be a txtQty control at that position? And if so, that the Text
can be stored as Integer?

You might want to turn on Option Strict and Option Explicit. You will
probably get a few compiler error, but once those are sorted out you
should get more stable code.
 
D

David Lozzi

One more thing,, this is in .Net 2.0, sorry for not saying that sooner.
Hopefully that doesnt matter.

Thanks,

--
David Lozzi
dlozzi@(remove)delphi-ts.com
www.delphi-ts.com


What error do you get?

Are you certain the text in the control can be cast to TextBox? Or that
there will be a txtQty control at that position? And if so, that the Text
can be stored as Integer?

You might want to turn on Option Strict and Option Explicit. You will
probably get a few compiler error, but once those are sorted out you
should get more stable code.
 
M

Morten Wennevik

Interesting,

In C# this works fine, but in VB.Net 2.0 it does not. There appears to be
some differences between the C# for loop and the VB.Net version. In
VB.Net the bounds for the for to loop appears to be fixed.

You can overcome this by doing an extra check

If (i >= bag.Count - 1) Then
Exit For
End If

Dim item As BagItem = bag(i)

I'm sure there is a logical explanation for this. You might want to ask
in the VB group

microsoft.public.dotnet.languages.vb
 

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

Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top