VB中的Dictionary对象

VB中的Dictionary对象

Dictionary对象不是VBA或Visual Basic实时语言的具体存在的部分,它是存在于Microsoft Scripting Runtime Library(SCRRUN.DLL)中的一个对象。

为了在应用程序中使用Dictionary对象,就必须利用Reference对话框增加一个项目级的引用到Scripting Runtime Library。

核心归纳:

1、更换键名,用obj.key("xx")="newxx"

2、更换键值或访问指定键:

(1)重设键值:obj.item("xx")="newxx",

(2)取得键值:obj.item("xx")="newxx" 中的KEY键“xx”未设定时,会在对象的后面增加一个键值对。

3、获得条目数:obj.count (从1开始)

4、增加键值对:obj.add key,value

5、移除键: obj.remove("key") , obj.removeall()

6、判定键是否存在:obj.Exists("key")

7、遍历:先把键和值各自赋给一个变量,构成一维数组,再用FOR遍历

a=d.keys 'b=d.Items

For i=0 to d.count-1

a(i)

b(i)

Next

8、用obj..CompareMode = 0(二进制,默认,大小写敏感)或= 1(文本,大小写不区分,但不会后面的覆盖前面)来指定比较模式,

'-----------------------------------

(1) Dim D as new Dictionnary

(2) Dim D as Dictionnary

set D =new Dictionnary

(3) Dim D as Dictionnary

Set d = CreateObject("Scripting.Dictionary")

'-------------------

Dim a, d, i '创建一些变量

Set d = CreateObject("Scripting.Dictionary")

d.Add "a", "Athens" '添加一些关键字和条目。

d.Add "b", "Belgrade"

blnIsThere = d.Exists("Key1")

strItem = d.Item("Key2")

strItem = d.Remove("Key3")

d.RemoveAll

a = d.Items '取得条目

For i = 0 To d.Count -1 '重复数组

Print a(i) '打印条目

Next

arrKeys = D.Keys

For Each objItem in arrItems

x = arrItems(objItem)

Next

1.Dictionary对象

当增加一个键/条目对时,如果该键已存在;或者删除一个键/条目对时,该关键字/条目对不存在,或改变已包含数据的Dictionary对象的CompareMode,都将产生错误。

属性

CompareMode 设定或返回键的字符串比较模式

Count 只读。 返回Dictionary里的键/条目对的数量 ---从1开始,而不像数组从0开始计数

Item(key) 设定或返回指定的键的值

Key(key) 设定键名值

方法

Add(key,item) 增加键/条目对到Dictionary

Exists(key) 如果指定的键存在,返回True,否则返回False

Remove(key) 删除一个指定的键/条目对

RemoveAll() 删除全部键/条目对

Items() 返回一个包含Dictionary对象中所有条目的数组

Keys() 返回一个包含Dictionary对象中所有键的数组

2. 对Dictionary中增加和删除条目

一旦得到一个新的(空的)Dictionary,可以对其添加条目,从中获取条目以及删除条目:

blnIsThere = d.Exists("Key1")

strItem = d.Item("Key2")

strItem = d.Remove("Key3")

a.RemoveAll

3. 修改键或条目的值

可以通过修改键的值,或通过修改与特定的键关联的条目的数据,来改变存储在Dictionary内的数据。下面的代码改变键为MyKey的条目中的数据。

a.Item(“MyKey”) = “NewValue”

如果指定的键在Dictionary未找到,将在Dictionary中后面位置创建一个以MyKey为键,以New Value为其条目值的新的键/条目对。

有意思的是,如果使用一个不存在的键来检索条目,不仅得到一个空的字符串(这是可以想到的),而且还在Dictionary里添加一个新的键/条目对,键即是指定的键,但条目的数据为空。

可以使用Key属性仅改变键名的值而不改变与之对应的条目的数据。将一个已存在的键MyKey改变为MyNewKey,可以用:

a.Key(“MyKey”) = “MyNewValue”

如果指定的键未找到,则产生运行期错误。

4. 设置比较模式

Dictionary的CompareMode属性。

当比较字符串键时,允许指定比较的方式。两个允许的值为BinaryCompare(0)和TextCompare(1)。BinaryCompare(0)为二进制数对照(即区分大小写);TextCompare(1)为文本对照(即不区分大小写)。

5. 遍历Dictionary

研究Dictionary时,有两个方法和一个属性需要特别注意,它们允许我们遍历存储在Dictionary里的所有键/条目对。

Items方法用一个一维数组的形式返回Dictionary里所有的条目数据.

keys方法用一个一维数组返回所有已存在的键值。

可以使用Count属性得到键或条目的数量。

例如,可以使用下列代码得到名称为D的Dictionary中所有的键和条目值。注意,虽然Count属性保存了在Dictionary里的键/条目数量

数组下标应从0到Count-1。

arrKeys = D.Keys ‘Get all the keys into an array

arrItems = D.Items ‘Get all the items into an array

For intLoop = 0 To D.Count –1 ‘Iterate through the array

StrThisKey = arrKeys(intLoop) ‘This is the key value

StrThisItem = arrItems(intLoop) ‘This is the item (data) value

Next

也可以使用For Each … Next语句完成同样的功能:

arrKeys = D.Keys

For Each objItem in arrItems

x = arrItems(objItem)

Next