473,811 Members | 3,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Row s.Item(cnt).Fin dControl("txtQt y"),
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 2645
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.Row s.Item(cnt).Fin dControl("txtQt y"),
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.Row s.Item(cnt).Fin dControl("txtQt y"),
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.Row s.Item(i).FindC ontrol("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.comwro te in message
news:op******** *******@tr024.b ouvet.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.Row s.Item(cnt).Fin dControl("txtQt y"),
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.Row s.Item(i).FindC ontrol("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.Argument OutOfRangeExcep tion: 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.comwro te in message
news:op******** *******@tr024.b ouvet.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.Row s.Item(i).FindC ontrol("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.comwro te in message
news:op******** *******@tr024.b ouvet.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.Row s.Item(i).FindC ontrol("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.publi c.dotnet.langua ges.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.Argument OutOfRangeExcep tion: 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
2186
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 datagrid. I am using the viewstate object to store the Arraylist items on the page on postback. My PROBLEM is that I need to redirect the user to a new aspx page and on this new page i need to be able to access the items in my arraylist. Is this...
6
16991
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 through a dictionary, looking for things, then remove them.... John
7
34804
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 Martin Arvidsson
24
4410
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 we covered was within the last week of the class and all self taught. Our prof gave us an example of a Java method used to remove elements from an array: public void searchProcess() { int outIt=0;
7
1758
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 the entries over to delete one entry? All I want to do is remove an entry at a specified index and then have the ubound(array) go down -1.
6
6121
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 more minutes. 'REMOVE DUBLICATED VALUE FROM ARRAY +++++++++++++++++ Dim col As New Scripting.Dictionary Dim ii As Integer = 0
3
6980
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, "Standard UTM")) aSystem.Add(New PickList(-2, "UTM-NAD83")) aSystem.Add(New PickList(-3, "UTM-NAD27"))
1
1799
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 have any duplicates? This might be bass-ackwards, but I'm trying to get something to work really quick that I don't have time right now to research. I am able to sort but I keep getting index errors, which I assume are happening when I get to the...
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10393
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10405
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.