Arraylist acting like a Linked List

Hello,

I have been working on this all day and can't tell if this is a bug or an intended funciton of an arraylist. I don't believe I have ever run into it before.

I have an arraylist defined within a function and I need to add objects to it. I pass in an object to the function, update the object's properties, and add it to the arraylist. Then I update the object's properties again and add it again to the arraylist.

The problem is: When I update the variable's properties before adding it the second time,the arraylist's first item is also updated! I don't specifically reference the arraylist's first item... it's just getting updated when I modify the original object. I can't figure out how to get around this, since I need to add multiple copies of the item from within a loop.

Here is a simplified version of the code:

=========================================

Private Sub AddItemByLargestUofM(ByVal inItem As CCartItem, ByVal inQuantityEach As Integer)
Dim ItemsToAdd As New ArrayList
Dim StartQty As Decimal


StartQty = inQuantityEach

While StartQty > 0

inItem.Quantity = StartQty

ItemsToAdd.Add(inItem) 'If I do a Watch on the ItemsToAdd here, the first Item's Quantity has changed before I add the second Item

StartQty = StartQty - 5

'Do something with ItemsToAdd

End While

End Sub

=========================================

Ihope someone can help clear this up for me. I have some other options, but they are messy (such as copying the object by copying all of it's 20 or so properties)

Thank You,

~ Michelle

[2494 byte] By [ayachem] at [2007-12-24]
# 1

The problem is not with the arraylist, but most likely with your use of "reference types". If the inItem paramenter is indeed a Reference Type (Class), then you are actually adding the same reference multiple times to the list, so regardless of which one you use, you are modifying the same object.

To address your problem, you will need to create a shallow copy of your object (see http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemObjectClassMemberwiseCloneTopic.asp), or use a value type (Structure).

Hope this helps,

MSFTAbelValadez at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

You are wonderful! I don't know how I've gone this long without learning that, but I will never forget it. Thank you!

I added a function to my derived class called CCartItem.GetCopy() and had it return CType(MemberwiseClone(),CCartItem)

Using that new function it works beutifully.

Thank You,

~ Michelle

ayachem at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

That's part of the magic of OOP.

If you have a node in a tree view such as

dim nd as treenode = treeview1.selectednode

you can say, nd.text = "new Name"

and the name in the treeview will change. Or you do the same with

dim nd as treenode = treeview1.selectednode.text = "new Name"

ReneeC at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...