Solved

Finding the last item in a collection

Posted on 2003-11-07
4
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

756 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