Collection

  • Thread starter Thread starter Michel
  • Start date Start date
M

Michel

Please how to identify the las item of an itemcollection
without to scan all the collection.
 
Hi Michel,
When the collection index starts at zero with pseudo code
\\\\
Mostly myItem = collection.item(collection.length - 1)
////
And when the collection start at one of course without that -1

I hope this helps a little bit.

Cor
 
Scott M. said:
collection(collection.count-1)

Actually, numerically indexed collections have base 1, so this code (without
"minus one") would be the proper code to return the "item no. 3" text.

Dim aColl As Collection
aColl = New Collection()
aColl.Add("item no. 1")
aColl.Add("item no. 2")
aColl.Add("item no. 3")
MessageBox.Show(aColl(aColl.Count))
 
Simen,
Really depends on which Collection object you are referring to.

The Microsoft.VisualBasic.Collection object has this "off by one" feature
that you are referring to. This is to maintain compatibility with VB6.

All (that I know of) the collections in the framework (System.Array,
System.Collections.*, plus all the ones in Windows Forms, Web Forms,
ADO.NET) that allow indexing by integer operator as Scott M stated. Some
have Count, some have Length.

Because of this inherit "off by one" feature, plus it is designed to do
more, I normally do not use Microsoft.VisualBasic.Collection, instead
relying on the collections in System.Collections. As System.Collections
collections are consistent with the rest of the framework, and are more
lightweight & specialized collections.

Hope this helps
Jay
 
Thanks gentlemen.
I've forgotten the end of the request
what item from the collection Item???

collection(collection.count-1).text is better now

Thanks for your help
 
Back
Top