Metodo Count

Restituisce il numero di elementi in una raccolta o un oggetto Dictionary. Sola lettura.

Sintassi

object.Count

Argomenti

Object: obbligatorio. Sempre il nome di un oggetto Dictionary.

Note

Nel codice seguente viene illustrato l'uso della proprietà Count.

Esempio 1

Function ShowItemCount
    Dim d, count
    Set d = CreateObject("Scripting.Dictionary")

    ' Add some keys and items.
    d.Add "a", "Athens"
    d.Add "b", "Belgrade"
    d.Add "c", "Cairo"

    ' Get the item count.
    count = d.Count

    ShowItemCount = "The dictionary contains " & count & " items."
End Function

' Usage
Dim result
result = ShowItemCount
' Output: The dictionary contains 3 items.

Esempio 2

Function DisplayItemsWithCount
    Dim d, i, s
    Set d = CreateObject("Scripting.Dictionary")

    ' Add some keys and items.
    d.Add "x", "Xenon"
    d.Add "y", "Yttrium"
    d.Add "z", "Zirconium"

    ' Get the item count.
    Dim count
    count = d.Count

    ' Display each item.
    s = "The dictionary contains " & count & " items:"
    For Each Key In d.Keys
        s = s & "Key: " & Key & ", Item: " & d.Item(Key)
    Next

    DisplayItemsWithCount = s
End Function

' Usage
Dim result
result = DisplayItemsWithCount
' Output: The dictionary contains 3 items: Key: x, Item: Xenon Key: y, Item: Yttrium Key: z, Item: Zirconium

Esempio 3

Function CountAfterRemoval
    Dim d, count, s
    Set d = CreateObject("Scripting.Dictionary")

    ' Add some keys and items.
    d.Add "m", "Moscow"
    d.Add "n", "New York"
    d.Add "p", "Paris"

    ' Remove a key-item pair.
    d.Remove("n")

    ' Get the item count.
    count = d.Count

    ' Display the count and remaining items.
    s = "After removal, the dictionary contains " & count & " items:"
    For Each Key In d.Keys
        s = s & "Key: " & Key & ", Item: " & d.Item(Key)
    Next

    CountAfterRemoval = s
End Function

' Usage
Dim result
result = CountAfterRemoval
' Output: After removal, the dictionary contains 2 items: Key: m, Item: Moscow Key: p, Item: Paris