The items of a collection are organized in an arranged sequence where each item holds a specific index. the first item in the collection holds an index of 1. The second item holds an index of 2, and so on. To give you access to the items of a collection, the Collection class is equipped with a property named Item. There are two ways you can use this property. To formally use the Item property, type the name of the collection object, followed by the period operator, followed by Item and optional parentheses. After the Item property or inside its parentheses, type the index of the desired item. Here is an example: Sub Exercise() Dim Employees As Collection Set Employees = New Collection Employees.Add "Patricia Katts" Employees.Add "James Wiley" Employees.Add "Gertrude Monay" Employees.Add "Helene Mukoko" Employees.Item 2 End Sub Remember that you an also use parentheses: Sub Exercise() Dim Employees As Collection Set Employees = New Collection Employees.Add "Patricia Katts" Employees.Add "James Wiley" Employees.Add "Gertrude Monay" Employees.Add "Helene Mukoko" Employees.Item (2) End Sub Instead of using the Item property, you can apply the index directly to the collection object. Here are examples: Sub Exercise() Dim Employees As Collection Set Employees = New Collection Employees.Add "Patricia Katts" Employees.Add "James Wiley" Employees.Add "Gertrude Monay" Employees.Add "Helene Mukoko" Employees.Item 2 Employees.Item (2) Employees 2 Employees (2) End Sub All these four techniques (notations) give you access to the item whose index you provided.
As opposed to adding a new item, you can delete one. To support this operation, the Collection class is equipped with a method name Remove. Its syntax is: Public Sub Remove(Index As Integer) This method takes one argument. When calling it, pass the index of the item you want to delete. Here is an example: Sub Exercise() Dim Employees As Collection Set Employees = New Collection Employees.Add "Patricia Katts" Employees.Add "James Wiley" Employees.Add "Gertrude Monay" Employees.Add "Helene Mukoko" Employees.Remove 2 End Sub This code deletes the second item in the collection.
When you start a new collection, obviously it is empty and its number of items is 0. To keep track of the number of items in a collection, the Collection class is equipped with a property named Count whose type is an integer. Remember that all built-in collection classes inherit their behavior from the Collection class. This means that the built-in collection classes are equipped with a property named Count to hold their number of items. We saw how you can add new items to a collection. Every time you add a new item to the collection, the Count property increases by 1. We also know how to remove an item from a collection. Whenever an existing item is deleted, the value of the Count property is decreased by 1. At anytime, to know the number of items that a collection is currently holding, get the value of its Count property. |
|
|||||||
|