Countメソッド

コレクションまたはDictionaryオブジェクト内のアイテム数を戻します。読取り専用です。

構文

object.Count

引数

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

備考

次のコードは、Countプロパティの使用方法を示しています:

例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.

例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

例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