ASPディクショナリオブジェクト
Dictionaryオブジェクトは、名前と値のペアで情報を格納します。
その他の例
Dictionaryオブジェクトを作成し、Existsメソッドを使用して、指定されたキーが存在するかどうかを確認する方法。
のアイテム
の配列を返すItemsメソッドを使用してすべてのアイテムの配列を返す方法。
のキー
の配列を返すKeysメソッドを使用してすべてのキーの配列を返す方法。
の値を返すItemプロパティを使用してアイテムの値を返す方法。
を設定するKeyプロパティを使用してDictionaryオブジェクトにキーを設定する方法。
の数を返すCountプロパティを使用してキー/アイテムのペアの数を返す方法。
辞書オブジェクト
Dictionaryオブジェクトは、名前と値のペア(キーとアイテムと呼ばれる)で情報を格納するために使用されます。Dictionaryオブジェクトは配列に似ているように見えるかもしれませんが、Dictionaryオブジェクトは、関連データを操作するためのより望ましいソリューションです。
辞書と配列の比較:
- キーは、Dictionaryオブジェクト内のアイテムを識別するために使用されます
- Dictionaryオブジェクトのサイズを変更するためにReDimを呼び出す必要はありません
- 辞書からアイテムを削除すると、残りのアイテムは自動的に上にシフトします
- 辞書は多次元にすることはできません、配列はできます
- 辞書には、配列よりも多くの組み込み関数があります
- ランダムな要素に頻繁にアクセスする場合、辞書は配列よりもうまく機能します
- 辞書は、コンテンツによってアイテムを見つける際に配列よりもうまく機能します
次の例では、Dictionaryオブジェクトを作成し、それにいくつかのキーとアイテムのペアを追加して、キーgrのアイテム値を取得します。
<%
Dim d
Set d=Server.CreateObject("Scripting.Dictionary")
d.Add "re","Red"
d.Add "gr","Green"
d.Add "bl","Blue"
d.Add "pi","Pink"
Response.Write("The value of key gr is: " & d.Item("gr"))
%>
Output:
The value of key gr is: Green
Dictionaryオブジェクトのプロパティとメソッドを以下に説明します。
プロパティ
Property | Description |
---|---|
CompareMode | Sets or returns the comparison mode for comparing keys in a Dictionary object |
Count | Returns the number of key/item pairs in a Dictionary object |
Item | Sets or returns the value of an item in a Dictionary object |
Key | Sets a new key value for an existing key value in a Dictionary object |
メソッド
Method | Description |
---|---|
Add | Adds a new key/item pair to a Dictionary object |
Exists | Returns a Boolean value that indicates whether a specified key exists in the Dictionary object |
Items | Returns an array of all the items in a Dictionary object |
Keys | Returns an array of all the keys in a Dictionary object |
Remove | Removes one specified key/item pair from the Dictionary object |
RemoveAll | Removes all the key/item pairs in the Dictionary object |