设置或返回 Dictionary 对象中指定键的项。对于集合,根据指定键返回项。读/写。
语法
object.Item(key)[ = newitem]
参数:
Object:必需。始终为集合或 Dictionary 对象的名称。
Key:必需。与要检索或添加的项关联的键。
Newitem:可选。仅用于 Dictionary 对象;不适用于集合。如果提供,则 newitem 是与指定键关联的新值。
注释
如果更改项时未找到键,则使用指定的 newitem 创建新键。如果尝试返回现有项时未找到键,则创建新键,并将其对应项留空。
以下示例说明了 Item 属性的用法。
示例 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
示例 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
示例 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