Solved

Finding the last item in a collection

Posted on 2003-11-07
4
159 Views
Last Modified: 2010-05-03
I have a collection call colCustomers with a key element CustomerID.  CustomerID is in the sequence 0001, 0002, 0003 etc... However sometimes a key may be skipped eg: 0001, 0002, 0004, 0005, 0006.  I need to be able to find out what the last CustomerID is so that I can create a new one that is one number higher.  I don't want to create in a missing spot and I don't want to resort the collection and change every CustomerID so non are skipped.

Thanks, Jebus
0
Comment
Question by:dj__jebus
  • 2
4 Comments
 
LVL 18

Expert Comment

by:Sethi
ID: 9705768
Count the no. of items in the collection. The total count -1 would be the last item. example:

Collection.Item(Collection.Count-1)
0
 

Author Comment

by:dj__jebus
ID: 9705795
Well I have

NewCustomerID = colCustomers.Item(colCustomers.Count - 1).CustomerID + 1

and I get "This key is already asociated with an element of this collection" error.  I have tried this already and I think it does it because when a CustomerID is skipped the count is actually less than the highest CustomerID

Thanks, Jebus
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9706012
Why not just store the last used CustomerID externally in a variable and increment it?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 9706035
This is the major drawback of a collection...it takes a long time to find the last item since it has to walk the linked list item by item.  Also there is no way to do a reverse key lookup given an index.

An alternative scheme would be to store the last used customerID in the first spot in the collection!  The objects in a collection do not have to be of the same data type and it would not be hard to modify existing code to not use the first index.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question