473,396 Members | 1,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Removing items from an arraylist

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,

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

Aug 10 '06 #1
7 2618
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[i].ToString() == "15")
list.RemoveAt(i);
}

for(int i = 0; i <= list.Count - 1; i++)
{
object o = list[i];
if (o.ToString() == "15")
list.Remove(o);
}
On Thu, 10 Aug 2006 05:29:55 +0200, David Lozzi <dl****@nospam.nospam
wrote:
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,


--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 10 '06 #2
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

"David Lozzi" wrote:
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,

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

Aug 10 '06 #3
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
"Morten Wennevik" <Mo************@hotmail.comwrote in message
news:op***************@tr024.bouvet.no...
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[i].ToString() == "15")
list.RemoveAt(i);
}

for(int i = 0; i <= list.Count - 1; i++)
{
object o = list[i];
if (o.ToString() == "15")
list.Remove(o);
}
On Thu, 10 Aug 2006 05:29:55 +0200, David Lozzi <dl****@nospam.nospam>
wrote:
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,


--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 10 '06 #4
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.
On Thu, 10 Aug 2006 14:41:19 +0200, David Lozzi <dl****@nospam.nospam
wrote:
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


--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 10 '06 #5
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
"Morten Wennevik" <Mo************@hotmail.comwrote in message
news:op***************@tr024.bouvet.no...
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.
On Thu, 10 Aug 2006 14:41:19 +0200, David Lozzi <dl****@nospam.nospam>
wrote:
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


--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 10 '06 #6
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
"Morten Wennevik" <Mo************@hotmail.comwrote in message
news:op***************@tr024.bouvet.no...
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.
On Thu, 10 Aug 2006 14:41:19 +0200, David Lozzi <dl****@nospam.nospam>
wrote:
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


--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 10 '06 #7
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


On Thu, 10 Aug 2006 15:05:14 +0200, David Lozzi <dl****@nospam.nospam
wrote:
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)


--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 11 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
6
by: JohnK | last post by:
ok, ya got me here. I'm trying to removing items from a dictionary inside a loop. Obviously using enumeration does not work as that assumes the dictionary stays unchanged. So how so I iterate...
7
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I'v been struggeling with removing selected items from a listview. Anyone that can give me a piece of code that does this? I am a newbee to this C# and cant figure it out.... Regards...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
7
by: Adam Honek | last post by:
Is there a direct way to remove one entry from a one dimensional array? I keep looking but either my eyes are funny or it isn't there. Must we really create a temp array and copy all but 1 of...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
3
by: Stuart | last post by:
I am using Visual Basic 2005. I have created a two dimensional ArrayList named aSystem that is populated as follows:- aSystem.Add(New PickList(0, "Undefined")) aSystem.Add(New PickList(-1,...
1
by: AllBeagle | last post by:
Here is what might seem to be an odd request. Does anyone know how I can find a duplicate in an arraylist and delete both the duplicate and the original, leaving me with only the items that didn't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.