Scripting.Dictionary 对象

Back to Documentation Index

许多 Microsoft 的编程语言,如 Visual Basic、VBScript 和 Jscript,都提供集合(collection)。可以把集合想象为数组,可以使用其中内建的函数完成存储和操纵数据等基本任务。无须担心数据是在哪些行列,而是使用唯一的键进行访问。

VBScript 和 Jscript 都提供类似的对象,通称 Scripting.Dictionary 对象或 Dictionary 对象。它类似于二维数组,把键和相关条目的数据存放在一起。然而真正的面向对象的方法,不应直接访问数据条目,必须使用 Dictionary 对象支持的方法和属性来实现。

创建和使用 Dictionary 对象

创建一个 Dictionary 对象的示例如下:

'In VBScript:
Dim objMyData
Set objMyData = Server.CreateObject("Scripting.Dictionary")
//In Jscript:
var objMyData = Server.CreateObject('Scripting.Dictionary');
<!-- Server-Side with an OBJECT element -->
<OBJECT RUNAT="SERVER" SCOPE="PAGE" ID="objMyData" PROGID="Scripting.Dictionary"></OBJECT>

Dictionary 对象还可用于客户端的 IE 中。

  1. Dictionary 对象的成员概要

    表1和表2列出了 Dictionary 对象的属性和方法及相应的说明。

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

    表1 Dictionary 对象的属性和说明
    属性 说明
    CompareMode 设定或返回键的字符串比较模式(仅用于 VBScript)
    Count 只读。返回 Dictionary 里的键/条目对的数量
    Item(key) 设定或返回指定的键的条目值
    Key(key) 设定键值
    表2 Dictionary 对象的方法和说明
    方法 说明
    Add(key,item) 增加键/条目对到 Dictionary
    Exists(key) 如果指定的键存在,返回 True,否则返回 False
    Items() 返回一个包含 Dictionary 对象中所有条目的数组
    Keys() 返回一个包含 Dictionary 对象中所有键的数组
    Remove(key) 删除一个指定的键/条目对
    RemoveAll() 删除全部键/条目对
  2. 对 Dictionary 中增加和删除条目

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

    'In VBScript:
    objMyData.Add "MyKey", "MyItem" 'Add Value MyItem with key MyKey
    objMyData.Add "YourKey", "YourItem" 'Add value YourItem with key YourKey
    blnIsThere = objMyData.Exists("MyKey") 'Returns True because the item exists
    strItem = objMyData.Item("YourKey") 'Retrieve value of YourKey
    strItem = objMyData.Remove("MyKey") 'Retrieve and remove YourKey
    objMyData.RemoveAll 'Remove all the items
    // In JScript;
    objMyData.Add ('MyKey', 'MyItem'); //Add Value MyItem with key MyKey
    objMyData.Add ('YourKey', 'YourItem'); //Add value YourItem with key YourKey
    var blnIsThere = objMyData.Exists('MyKey'); //Returns True because the item exists
    var strItem = objMyData.Item('YourKey'); //Retrieve value of YourKey
    var strItem = objMyData.Remove('MyKey'); //Retrieve and remove YourKey
    objMyData.RemoveAll(); //Remove all the items
  3. 修改键或条目的值

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

    ObjMyData.Item("MyKey") = "NewValue" ' In VBScript ObjMyData.Item('MyKey') = 'NewValue'; // In JScript

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

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

    ObjMyData.Key("MyKey") = "NewValue" ' In VBScript ObjMyData.Key('MyKey') = 'NewValue'; // In JScript

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

  4. 设置比较模式

    Dictionary 的 CompareMode 属性仅适用于 VBScript,不能在 JScript 中使用。当比较字符串键时,允许指定比较的方式。两个允许的值为 BinaryCompare(0) 和 TextCompare(1)。 BinaryCompare(0) 为二进制数对照(即区分大小写); TextCompare(1) 为文本对照(即不区分大小写)。

  5. 遍历 Dictionary

    研究 Dictionary 时,有两个方法和一个属性需要特别注意,它们允许我们遍历存储在 Dictionary 里的所有键/条目对。 Items 方法用一个一维数组的形式返回 Dictionary 里所有的条目数据,而 keys 方法用一个一维数组返回所有已存在的键值。可以使用 Count 属性得到键或条目的数量。

    例如,可以使用下列代码得到名称为 objMyData 的 Dictionary 中所有的键和条目值。注意,虽然 Count 属性保存了在 Dictionary 里的键/条目数量,但 VBScript 和 JScript 的数组总是从下标 0 开始的。因此,数组下标应从 0 到 Count-1。

    'In VBScript:
    arrKeys = objMyData.Keys 'Get all the keys into an array
    arrItems = objMyData.Items 'Get all the items into an array

    For intLoop = 0 To objMyData.Count – 1 'Iterate through the array
        StrThisKey = arrKeys(intLoop) 'This is the key value
        StrThisItem = arrItems(intLoop) 'This is the item (data) value
    Next
    // In JScript
    // Get VB-style arrays using the Keys() and Items() methods
    var arrKeys = new VBArray(objMyData.Keys()).toArray();
    var arrItems = new VBArray(objMyData.Items()).toArray();

    for (intLoop = 0; intLoop < objMyData.Count; intLoop++) {
        // Iterate through the arrays
        strThisKey = arrKeys[intLoop]; // This is the key value
        strThisItem = arrItems[intLoop]; // This is the item (data) value
    }

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

    ' Iterate the dictionary as a collection in VBScript
    For Each objItem in arrItems
        Response.Write objItem & " = " & arrItems(objItem) & "<br>"
    Next