Aggiunge una coppia chiave/elemento a un oggetto Dictionary.
Sintassi
object.Add(key, item)
Argomenti:
Object: obbligatorio. Sempre il nome di un oggetto Dictionary.
Key: obbligatorio. Chiave associata all'elemento da aggiungere.
Item: obbligatorio. Elemento associato alla chiave da aggiungere.
Note
Si verifica un errore se la chiave esiste già.
Nell'esempio seguente viene illustrato l'uso del metodo Add.
Esempio 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
Esempio 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
Esempio 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:
L'utilizzo diretto della creazione di nuovi oggetti nel dizionario non è supportato.
Ad esempio:
Set d = CreateObject("Scripting.Dictionary")
'Create an object first and then add it to the dictionary.
Set obj = new class
d.add key, obj è supportato.