Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is wrong here? Looping through collection giving bizarre results.

Status
Not open for further replies.

MissouriTiger

Programmer
Oct 10, 2000
185
US
Pelase help if you can. I have a deadline, and have spent hours wrestling with this problem.

My program populates a collection with over 1000 product objects, using the product ID as the key for each Product object. The product ID is also stored in a variable in the product object. Each product has a unique ID.

As each product is added to the collection, the product ID is being written to a text file, just for the sake of debugging, which proves that the value for the iD is correct. Also, all 1003 products are added to the collection with no errors.

Here's where the problem comes in: Immediately after building the collection, I use a loop to get the product IDs for all the 1003 products, and the exact same product ID is returned for all 1003 of them. The ID that is being returned happens to be the ID of the last product added.

But I KNOW they don't all have the same ID, and I have a text file to prove it. And they certainly don't have the same key, because that's impossible. And the key is the ID.

Is there some trick to loopig through a collection? I've looked at code on the Web, and I don't see anything special that needs to be done. Is there some kind of cursor that gets stuck on 1 item and won't iterate?

Here's my code:

-----------------------------------------
Code to add the product to the collection:
-----------------------------------------

'inside a loop

objProductCollection.Add(objProduct, objProduct.getProductNumberBasic())

'NOTE: If the product IDs were all the same, that above line of code would crash, so we know they are unique. So why doesn't the code below reflect that?

-----------------------------------
Code to loop through the collection:
-----------------------------------

For i = 1 To objProductCollection.Count()

My.Computer.FileSystem.WriteAllText("C://getProductNumberBasic.txt", objProductCollection.Item(i).getProductNumberBasic() & vbCrLf, True)

Next

-----------------------------------------------

I also tried the above loop with a For Each, with no success.

This seems impossible, but it's happening. Can someone pelase set me straight on this?














Greg Norris
Software Developer & all around swell guy


__________________________________________________
Constructed from 100% recycled electrons.
 
I finally found the problem. Only one product object was being instantiated, and was being added to the collection a thousand times, and each time the properties were being updated.

Doh!





Greg Norris
Software Developer & all around swell guy


__________________________________________________
Constructed from 100% recycled electrons.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top