Item Method
Sets or returns an item for a specified key in a Dictionary object. For collections, returns an item based on the specified key. Read/write.
Syntax
object.Item(key)[ = newitem]
Arguments:
-
Object: Required. Always the name of a collection or Dictionary object.
-
Key: Required. Key associated with the item being retrieved or added.
-
Newitem: Optional. Used for Dictionary object only; no application for collections. If provided,
newitem
is the new value associated with the specified key.
Remarks
If key is not found when changing an item, a new key is created with the specified
newitem
. If key is not found when attempting to return an
existing item, a new key is created and its corresponding item is left empty.
The following example illustrates the use of the Item
property.
Example 1:
Function GetItem
Dim d ' Create some variables.
Set d = CreateObject("Scripting.Dictionary")
' Add some keys and items.
d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
' Get the item for key "c".
GetItem = d.Item("c")
End Function
' Usage
Dim result
result = GetItem()
' Output: Cairo
Example 2:
Function SetExistingItem
Dim d ' Create some variables.
Set d = CreateObject("Scripting.Dictionary")
' Add some keys and items.
d.Add "x", "Xenon"
d.Add "y", "Yttrium"
d.Add "z", "Zirconium"
' Set a new item for key "y".
d.Item("y") = "Yosemite"
' Get the item for key "y".
SetExistingItem = d.Item("y")
End Function
' Usage
Dim result
result = SetExistingItem()
' Output: Yosemite
Example 3:
Function AddNewItem
Dim d ' Create some variables.
Set d = CreateObject("Scripting.Dictionary")
' Add some keys and items.
d.Add "m", "Moscow"
d.Add "n", "New York"
d.Add "t", "Tokyo"
' Attempt to get an item for a non-existing key and set a new item.
d.Item("p") = "Paris"
' Get the item for the newly added key "p".
AddNewItem = d.Item("p")
End Function
' Usage
Dim result
result = AddNewItem()
' Output: Paris