2021/12/07(Tue) 13:00:47 編集(投稿者)
Function Test()
' Collectionの場合
Dim MyClasses1 As New Collection
MyClasses1.Add "item1", "key1"
MyClasses1.Add "item2", "key2"
Debug.Print MyClasses1("key1")
' MyClasses1("key1") = "item1-E" 'エラーになる
' Debug.Print MyClasses1("key1")
' Dictionaryの場合
Dim MyDictionary1
Set MyDictionary1 = CreateObject("Scripting.Dictionary")
MyDictionary1.Add "key1", "item1"
MyDictionary1.Add "key2", "item2"
Debug.Print MyDictionary1("key1")
MyDictionary1("key1") = "item1-E"
Debug.Print MyDictionary1("key1")
' Collectionの場合キーがString以外だとエラーになる
' Dim MyClasses2 As New Collection
' MyClasses2.Add "item1", 1
' MyClasses2.Add "item2", 2
' Dictionaryの場合キーがString以外でもOK
Dim MyDictionary2
Set MyDictionary2 = CreateObject("Scripting.Dictionary")
MyDictionary2.Add 1, "item1"
MyDictionary2.Add 2, "item2"
' Collectionの中にCollectionを追加
Dim MyClasses3 As New Collection
MyClasses3.Add New Collection, "key1"
MyClasses3.Add New Collection, "key2"
MyClasses3("key1").Add "item1-1", "key1-1"
MyClasses3("key1").Add "item1-2", "key1-2"
MyClasses3("key2").Add "item2-1", "key2-1"
MyClasses3("key2").Add "item2-2", "key2-2"
Debug.Print MyClasses3("key1")("key1-1")
Debug.Print MyClasses3("key2")("key2-2")
'MyClasses3("key2")("key2-2") = "item2-2-E" 'エラーになる
'Debug.Print MyClasses3("key2")("key2-2")
' Dictionaryの中にDictionaryを追加
Dim MyDictionary3
Set MyDictionary3 = CreateObject("Scripting.Dictionary")
MyDictionary3.Add "key1", CreateObject("Scripting.Dictionary")
MyDictionary3.Add "key2", CreateObject("Scripting.Dictionary")
MyDictionary3("key1").Add "key1-1", "item1-1"
MyDictionary3("key1").Add "key1-2", "item1-2"
MyDictionary3("key2").Add "key2-1", "item2-1"
MyDictionary3("key2").Add "key2-2", "item2-2"
Debug.Print MyDictionary3("key1")("key1-2")
Debug.Print MyDictionary3("key2")("key2-2")
MyDictionary3("key2")("key2-2") = "item2-2-E"
Debug.Print MyDictionary3("key2")("key2-2")
Dim arrayVal() As Integer
' Collectionの中に配列を追加、ただし配列の要素数変更が直接できない
Dim MyClasses4 As New Collection
ReDim arrayVal(0 To 1)
arrayVal(0) = 0
arrayVal(1) = 1
MyClasses4.Add arrayVal, "key1"
ReDim arrayVal(0 To 2)
arrayVal(0) = 2
arrayVal(1) = 3
arrayVal(2) = 4
MyClasses4.Add arrayVal, "key2"
Debug.Print MyClasses4("key1")(0)
Debug.Print MyClasses4("key2")(0)
MyClasses4("key2")(0) = 10
Debug.Print MyClasses4("key2")(0) '値の変更はされない
' Dictionaryの中に配列を追加、ただし配列の要素数変更が直接できない
Dim MyDictionary4
Set MyDictionary4 = CreateObject("Scripting.Dictionary")
ReDim arrayVal(0 To 1)
arrayVal(0) = 5
arrayVal(1) = 6
MyDictionary4.Add "key1", arrayVal
ReDim arrayVal(0 To 2)
arrayVal(0) = 7
arrayVal(1) = 8
arrayVal(2) = 9
MyDictionary4.Add "key2", arrayVal
Debug.Print MyDictionary4("key1")(0)
Debug.Print MyDictionary4("key2")(0)
MyDictionary4("key2")(0) = 20
Debug.Print MyDictionary4("key2")(0) '値の変更はされない
' Collectionの中にDictionaryを追加
Dim MyClasses5 As New Collection
MyClasses5.Add CreateObject("Scripting.Dictionary"), "key1"
MyClasses5.Add CreateObject("Scripting.Dictionary"), "key2"
MyClasses5("key1").Add "key1-1", "item1-1"
MyClasses5("key1").Add "key1-2", "item1-2"
MyClasses5("key2").Add "key2-1", "item2-1"
MyClasses5("key2").Add "key2-2", "item2-2"
Debug.Print MyClasses5("key1")("key1-1")
' Dictionaryの中にCollectionを追加
Dim MyDictionary5
Set MyDictionary5 = CreateObject("Scripting.Dictionary")
MyDictionary5.Add "key1", New Collection
MyDictionary5.Add "key2", New Collection
MyDictionary5("key1").Add "item1-1", "key1-1"
MyDictionary5("key1").Add "item1-2", "key1-2"
MyDictionary5("key2").Add "item2-1", "key2-1"
MyDictionary5("key2").Add "item2-2", "key2-2"
Debug.Print MyDictionary5("key1")("key1-2")
End Function