Name
Collection.Item Method
Class
Microsoft.VisualBasic.Collection
Syntax
objectvariable
.Item(index
)
-
objectvariable
Use: Required
Data Type: Collection Object
An object variable of type Collection
-
index
Use: Required
Data Type: Integer or String
Either the index (the ordinal position) of the object in the collection, or the unique key name belonging to the object
Description
Returns the member of the collection for the specified key or ordinal position.
Programming Tips and Gotchas
When writing wrapper classes for collections, you can make your object model more readable by making the name of the property that wraps the Item method the same as the name of the object obtained from the collection. For example, if your collection class is called Employees and is a collection of Employee records, your object model reads much better to have an Employee Property procedure, as follows:
Public Property Employee(vKey as Object) As Boolean Get Employee = mcolEmployees.Item(vKey) End Get . . . End Property
Note that in the previous Property procedure, the parameter is passed as an object so that the argument can be either a string (the item’s key) or an integer (the item’s ordinal position).
There is no Exists method in the Collection object, so you cannot find out in advance if a particular key exists within the collection. However, you can create an
Exists
function by calling the Item method with a given key and returning an appropriate value based on whether an error occurred, as the following ...
Get VB .NET Language in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.