Solved

Finding the last item in a collection

Posted on 2003-11-07
4
162 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 86

Expert Comment

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

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

615 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