Addメソッド

Dictionaryオブジェクトにキーとアイテムのペアを追加します。

構文

object.Add(key, item)

引数:

  • Object: 必須。常にDictionaryオブジェクトの名前。

  • Key: 必須。追加するアイテムに関連付けられたキー。

  • Item: 必須。追加するキーに関連付けられたアイテム。

備考

キーがすでに存在する場合、エラーが発生します。

次の例では、Addメソッドの使用方法を示します。

例1:

Dim d   ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")

' Add some keys and items.
d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
' Output: Dictionary Object Contains Keys: a,b,c Items: Athens,Belgrade,Cairo

例2:

Dim d
Set d = CreateObject("Scripting.Dictionary")

' Add some keys and items.
If Not d.Exists("a") Then
    d.Add "a", "Athens"
End If

If Not d.Exists("b") Then
    d.Add "b", "Belgrade"
End If

If Not d.Exists("c") Then
    d.Add "c", "Cairo"
End If
' Output: Dictionary Object Contains Keys: a,b,c Items: Athens,Belgrade,Cairo

例3:

Dim d
Set d = CreateObject("Scripting.Dictionary")

Dim arrKeys, arrItems
arrKeys = Array("a", "b", "c")
arrItems = Array("Athens", "Belgrade", "Cairo")

For i = 0 To UBound(arrKeys)
    d.Add arrKeys(i), arrItems(i)
Next
' Output: Dictionary Object Contains Keys: a,b,c Items: Athens,Belgrade,Cairo

Note:

ディクショナリ追加コールでの新規オブジェクト作成の直接使用はサポートされていません。

例:

Set d = CreateObject("Scripting.Dictionary")

'Create an object first and then add it to the dictionary.

Set obj = new class

d.add key, obj これはサポートされています。