Add方法:给Dictionary对象添加键值和项目。键值不能重复,否则运行出错。
Dim dic
Set dic = CreateObject("Scripting.Dictionary")
dic.Add "key1","value1" '添加键值和项目
dic.Add "name","John"
dic.Add "sex","male"
Item属性:设置或返回指定键值的项目。键值可以重复,如果键值存在,更新对应项目的
内容,否则添加新的键值和项目。
Dim dic
Set dic = CreateObject("Scripting.Dictionary")
dic.Add "key1","value1" '添加键值和项目
dic.Add "name","John"
dic.Add "sex","male"
dic.Item("country")="China" '添加键值和项目
MsgBox dic.Item("country") '获取键值为country的项目
dic.Item("name")="Eric" '修改键值为name的项目
MsgBox dic.Item("name")
Count属性:返回Dictionary对象中键值及其项目的数量。
Dim dic
Set dic = CreateObject("Scripting.Dictionary")
dic.Add "key1","value1" '添加键值和项目
dic.Add "name","John"
dic.Add "sex","male"
MsgBox dic.Count '显示3
Key方法:修改Dictionary对象中对应键值的值
Dim dic
Set dic = CreateObject("Scripting.Dictionary")
dic.Add "key1","value1" '添加键值和项目
dic.Add "name","John"
dic.Add "sex","male"
MsgBox dic.Exists("key1") '显示True
dic.Key("key1")="newKey"
MsgBox dic.Exists("key1") '显示False
MsgBox dic.Exists("newKey") '显示True
If dic.Exists("newKey") Then
MsgBox dic.Item("newKey") '显示Value1
End If