Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

Finding the last item in a collection

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
dj__jebus
Asked:
dj__jebus
  • 2
1 Solution
 
SethiCommented:
Count the no. of items in the collection. The total count -1 would be the last item. example:

Collection.Item(Collection.Count-1)
0
 
dj__jebusAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Why not just store the last used CustomerID externally in a variable and increment it?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now