Collection.Item Method

   
Collection.Item Method

Class

Microsoft.VisualBasic.Collection

Syntax

   objectvariable   .Item(   index   ) 
objectvariable (required; Collection Object)

An object variable of type Collection

index (required; 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 code shows:

     Public Function Exists(ByVal oKey As Object) As Boolean     Try         moValue = mCollection.Item(oKey)         Exists = True     Catch e As NullReferenceException         Exists = False     End Try End Function 
  • The Item method is the default member of the Collection object, and since it is parameterized, we do not need to include an explicit call to the Item method. The following two statements, for example, are identical to one another:

     set objMember = objCollection.Item(6) set objMember = objCollection(6) 

See Also

Collection Class, Collection.Add Method, Collection.Count Property, Collection.Remove Method

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net