Fügt einem Dictionary-Objekt ein Schlüssel/Element-Paar hinzu.
Syntax
object.Add(key, item)
Argumente:
Object: Erforderlich. Immer der Name eines Dictionary-Objekts.
Key: Erforderlich. Der Schlüssel, der dem hinzuzufügenden Element zugeordnet ist.
Item: Erforderlich. Das Element, das dem hinzuzufügenden Schlüssel zugeordnet ist.
Anmerkungen
Ein Fehler tritt auf, wenn der Schlüssel bereits vorhanden ist.
Das folgende Beispiel veranschaulicht die Verwendung der Add-Methode.
Beispiel 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
Beispiel 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
Beispiel 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:
Die direkte Verwendung der Erstellung neuer Objekte im Dictionary-Aufruf "Add" wird nicht unterstützt.
Beispiel:
Set d = CreateObject("Scripting.Dictionary")
'Create an object first and then add it to the dictionary.
Set obj = new class
d.add key, obj Das wird unterstützt.