Dictionary オブジェクトの宣言
Dictionary オブジェクトの宣言は以下となる。(CreateObject 関数を使う)Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
参照設定を使用して宣言するする場合は以下のようになる。
1. メニューバーのツール → 参照設定 を選択
2. Microsoft Scripting Runtime にチェックを入れる
Dim dict As New Dictionary
要素の追加
Add メソッドを使用して要素(キーと値)を追加する。キーが既に存在する場合はエラーとなるので、要素を追加する前に後述する"キーの存在確認"をしておくと安全。
object.Add key, item
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
'要素を追加
dict.Add "aaa", 111 'キー: aaa, 値: 111
dict.Add "bbb", 222 'キー: bbb, 値: 222
dict.Add "ccc", 333 'キー: ccc, 値: 333
また、Add メソッド以外にも Item プロパティによる要素の追加も可能。← 値の変更 (要素を追加) の項を参照。
要素数の取得
Count プロパティを使用して要素の数を取得する。返り値はlong型。'要素を追加
dict.Add "aaa", 111 'キー: aaa, 値: 111
dict.Add "bbb", 222 'キー: bbb, 値: 222
dict.Add "ccc", 333 'キー: ccc, 値: 333
'要素数を取得
Debug.Print dict.Count
実行結果 3
キーの存在確認
Exists メソッドを使用してキーの存在確認ができる。返り値はBoolean型(True/False)。'要素を追加
dict.Add "aaa", 111 'キー: aaa, 値: 111
dict.Add "bbb", 222 'キー: bbb, 値: 222
'キーの存在確認
Debug.Print dict.Exists("aaa")
Debug.Print dict.Exists("bbb")
Debug.Print dict.Exists("ccc")
実行結果True
True
False
キーの変更
Key プロパティを使用してキーの変更ができる。存在しないキーを指定した場合はエラーとなるので注意。もちろん代入値に存在するキーを指定した場合もエラーとなる。
object.Key (key) = newkey
'要素を追加
dict.Item("aaa") = 111 'キー: aaa, 値: 111
dict.Item("bbb") = 222 'キー: bbb, 値: 222
'キーを変更
dict.Key("bbb") = "ccc" 'bbb -> ccc
'値を取得
Debug.Print dict.Item("aaa")
Debug.Print dict.Item("ccc")
実行結果 111
222
値の取得
Item プロパティを使用して、指定したキーの値を取得する。存在しないキーを指定した場合は、指定したキーの要素(値は空っぽ)が追加されてしまうので注意。
object.Item(key)
'要素を追加
dict.Add "aaa", 111 'キー: aaa, 値: 111
dict.Add "bbb", 222 'キー: bbb, 値: 222
dict.Add "ccc", 333 'キー: ccc, 値: 333
'要素を取得
Debug.Print dict.Item("aaa")
Debug.Print dict.Item("bbb")
Debug.Print dict.Item("ccc")
実行結果 111
222
333
値の変更 (要素を追加)
Item プロパティを使用して値の変更を行う。存在しないキーを指定した場合は、指定したキーの要素(値は指定値)が追加される。つまり、Item メソッドは 要素の追加 としても使える。(既にキーが存在していた場合でもエラーとならなずに上書きする)
object.Item(key) [= newitem]
'要素を追加
dict.Item("aaa") = 111 'キー: aaa, 値: 111
dict.Item("bbb") = 222 'キー: bbb, 値: 222
'要素を変更
dict.Item("bbb") = 333 '222 -> 333
'要素を取得
Debug.Print dict.Item("aaa")
Debug.Print dict.Item("bbb")
実行結果 111
333
キーを配列で取得
keys メソッドを使用して全てのキーを配列で取得する。Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim arrKeys As Variant
'要素を追加
dict.Item("aaa") = 111 'キー: aaa, 値: 111
dict.Item("bbb") = 222 'キー: bbb, 値: 222
dict.Item("ccc") = 333 'キー: ccc, 値: 333
'全てのキーを配列に代入
arrKeys = dict.Keys
'イミディエイトに出力
For Each temp In arrKeys
Debug.Print temp; ":"; dict.Item(temp)
Next
実行結果 aaa: 111
bbb: 222
ccc: 333
値を配列で取得
Items メソッドを使用して全ての値を配列で取得する。Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim arrItems As Variant
'要素を追加
dict.Item("aaa") = 111 'キー: aaa, 値: 111
dict.Item("bbb") = 222 'キー: bbb, 値: 222
dict.Item("ccc") = 333 'キー: ccc, 値: 333
'全ての値を配列に代入
arrItems = dict.Items
'イミディエイトに出力
For Each temp In arrItems
Debug.Print temp
Next
実行結果 111
222
333
要素を削除
Remove メソッドを使用して指定したキーの要素を削除する。RemoveAll メソッドを使用すると全ての要素を削除する。
'要素を3つ追加
dict.Item("aaa") = 111 'キー: aaa, 値: 111
dict.Item("bbb") = 222 'キー: bbb, 値: 222
dict.Item("ccc") = 333 'キー: ccc, 値: 333
'要素の削除
dict.Remove "bbb"
'要素数を取得 -> イミディエイト出力
Debug.Print dict.Count
'全要素の削除
dict.RemoveAll
'要素数を取得 -> イミディエイト出力
Debug.Print dict.Count
実行結果 2
0
要素の値を複数管理
1つのキーで複数の値を管理したい場合は、値を配列で入れてあげれば問題ない。はず。もっと賢いやり方があるかもしれない。
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim arrKey As Variant, arrItem As Variant
Dim key, item
'要素を追加 (値には配列を使う)
dict.item("aaa") = Array(11, 12, 13)
dict.item("bbb") = Array(21, 22, 23)
dict.item("ccc") = Array(31, 32, 33)
'全てのキーを配列に代入
arrKey = dict.Keys
'イミディエイトに出力
For Each key In arrKey
'配列の値を取得
arrItem = dict.item(key)
Debug.Print key; ":"; arrItem(0); arrItem(1); arrItem(2)
Next
実行結果aaa: 11 12 13
bbb: 21 22 23
ccc: 31 32 33
0 件のコメント:
コメントを投稿